Created
October 19, 2012 01:53
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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