Skip to content

Instantly share code, notes, and snippets.

@thisgeek
Created May 10, 2010 15:18
Show Gist options
  • Select an option

  • Save thisgeek/396169 to your computer and use it in GitHub Desktop.

Select an option

Save thisgeek/396169 to your computer and use it in GitHub Desktop.
jQuery.fn.resizeWithWindow = function(options) {
var settings = $.extend($.fn.resizeWithWindow.defaults, options),
element = $(this);
adjustSize(element);
$(window).resize(function() {
adjustSize(element);
});
function adjustSize(targets) {
return targets.each(function() {
var target = {
height: $(this).height(),
width: $(this).width()
},
browser = {
height: $(window).height(),
width: $(window).width()
},
ratio = {
height: target.height / browser.height,
width: target.width / browser.width
};
if (ratio.height > ratio.width) {
$(this).css({
'width': 'auto',
'height': '100%'
});
settings.container
.height(browser.height)
.width($(this).width() + settings.offset_width);
} else {
settings.container
.css('width', 'auto')
.height($(this).height());
$(this)
.css({
'width': '100%',
'height': 'auto'
});
}
return false;
});
}
};
jQuery.fn.resizeWithWindow.defaults = {
container: $(this).parent(),
offset_width: 0
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment