Skip to content

Instantly share code, notes, and snippets.

@corpix
Created September 17, 2011 23:19
Show Gist options
  • Save corpix/1224480 to your computer and use it in GitHub Desktop.
Save corpix/1224480 to your computer and use it in GitHub Desktop.
Proprotionate size changing(clientside)
function resizeImgOO(el){
function imgRatio(){
return (el.height / el.width);
}
function holderRatio(){
return (el.offsetParent.offsetHeight / el.offsetParent.offsetWidth);
}
function fitToContainer(){
if(imgRatio>holderRatio){
el.height = el.offsetParent.offsetHeight;
} else {
el.width = el.offsetParent.offsetWidth;
}
}
this.imgRatio = imgRatio;
this.holderRatio = holderRatio;
this.resize = fitToContainer;
}
var img = new resizeImgOO(document.getElementById('yourImgId'));
img.resize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment