Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created October 19, 2012 01:53
Show Gist options
  • Save ttscoff/3915837 to your computer and use it in GitHub Desktop.
Save ttscoff/3915837 to your computer and use it in GitHub Desktop.
Quick function to check viewport size and chop/crop images (CSS3) for mobile views.
var bt = {}; // to call the functions outside of the jQuery wrapper because OctoPress is a bit of a JS mess. Hey, that rhymes.
(function($){
bt = {
checkWinWidth: function() {
var width = $(window).width();
if (width <= 500) {
if (!$('body').hasClass('mobile')) {
$('img').each(function(){
var $this = $(this);
$this.replaceWith($('<div class="blowup" style="background-image:url(' + $this.attr('src') + '); background-size: cover; background-position:50% 50%;background-repeat:no-repeat;height:200px;width:100%;" /></div>').data('orig',$this));
});
$('body').addClass('mobile');
}
} else {
if ($('body').hasClass('mobile')) {
$('div.blowup').each(function(){
var $this = $(this);
$this.replaceWith($this.data('orig'));
});
$('body').removeClass('mobile');
}
}
}
};
$(window).resize(function(){
bt.checkWinWidth();
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment