Skip to content

Instantly share code, notes, and snippets.

@drrobotnik
Created February 4, 2014 01:19
Show Gist options
  • Save drrobotnik/8795803 to your computer and use it in GitHub Desktop.
Save drrobotnik/8795803 to your computer and use it in GitHub Desktop.
ratio
function aspectRatioRespond(){
mobileMax = 480;
elements = jQuery('[data-ratio]');
elements.each(function(){
container = jQuery(this);
var elRatio = container.data('ratio');
var elMobileRatio = ( typeof container.data('mobile-ratio') == 'string' ) ? container.data('mobile-ratio') : container.data('ratio');
if(container.width() < mobileMax){
autoHeight = elMobileRatio;
}else{
autoHeight = elRatio;
}
ratio = autoHeight.match(/(\d+)\:(\d+)/);
ratio = ratio[1] / ratio[2];
container.height( container.width() / ratio );
});
}
$(window).on('load resize orientationchange',aspectRatioRespond);
aspectRatioRespond();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment