Created
February 20, 2011 23:23
-
-
Save walterdavis/836415 to your computer and use it in GitHub Desktop.
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
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