Skip to content

Instantly share code, notes, and snippets.

@joshmvandercom
Created March 29, 2010 23:59
Show Gist options
  • Save joshmvandercom/348575 to your computer and use it in GitHub Desktop.
Save joshmvandercom/348575 to your computer and use it in GitHub Desktop.
$(document).ready(function(){
$('.js_resize').each(function(){
var imageParent = $(this).parent();
var myWidth = $(this).width();
var myHeight = $(this).height();
var parentWidth = $(this).parent().width();
var parentHeight = $(this).parent().height();
var offset, ratio, newSide;
if (myWidth>myHeight) {
if ($(this).height()>parentHeight) {
var ratio = parentHeight / $(this).height();
} else {
var ratio = $(this).height() / parentHeight;
}
newSide = ratio * $(this).width();
$(this).height(parentHeight);
if (newSide>parentWidth) {
$(this).css({'margin-left': parseInt((newSide - parentWidth)) * -1});
} else {
$(this).css({'margin-left': parseInt((parentWidth - newSide)) * -1});
}
} else if (myWidth<myHeight) {
if ($(this).width()>parentWidth) {
var ratio = parentWidth / $(this).width();
} else {
var ratio = $(this).width() / parentWidth;
}
newSide = ratio * $(this).height();
$(this).width(parentWidth);
if (newSide>parentHeight) {
$(this).css({'margin-top': parseInt((newSide - parentHeight)) * -1});
} else {
$(this).css({'margin-top': parseInt((parentHeight - newSide)) * -1});
}
} else {
$(this).height(parentWidth);
}
if ($(this).parent().hasClass('corners5')) {
$(this).parent().append('<div class="tr"></div><div class="tl"></div><div class="br"></div><div class="bl"></div>');
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment