Skip to content

Instantly share code, notes, and snippets.

@walterdavis
Created February 20, 2011 23:23
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save walterdavis/836415 to your computer and use it in GitHub Desktop.
Element.addMethods({
resizeImg: function(element){
var element = $(element);
var img = element.down('img');
var imgDims = img.getDimensions();
var ratio = imgDims['height']/imgDims['width'];
var winDims = document.viewport.getDimensions();
if ((winDims['height']/winDims['width']) > ratio) {
winDims['width'] = Math.ceil(winDims['height'] / ratio);
} else {
winDims['height'] = Math.ceil(winDims['width'] * ratio);
}
$A([img, element]).invoke('setStyle',{
height: winDims['height'] + 'px',
width: winDims['width'] + 'px'
});
return element;
}
});
//change this variable to match your layout
var origDiv = $('theIdOfYourImageDiv').resizeImg();
//resize the image on browser resize
Event.observe(window, 'resize', function(){ origDiv.resizeImg(); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment