Skip to content

Instantly share code, notes, and snippets.

@neekey
Created August 24, 2013 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neekey/6327058 to your computer and use it in GitHub Desktop.
Save neekey/6327058 to your computer and use it in GitHub Desktop.
根据容器宽高和当前图片宽高确定适应宽高
/**
* 根据容器宽高和当前图片宽高确定适应宽高
* @param conW 容器宽度
* @param conH 容器高度
* @param imgW 图片宽度
* @param imgH 图片高度
* @returns {{w: *, h: *}}
*/
getImgResize: function( conW, conH, imgW, imgH ){
if( imgW <= conW && imgH <= conH ){
return {
w: imgW,
h: imgH
};
}
else {
var width = imgW * 1.0;
var height = imgH * 1.0;
if(( width / conW ) > ( height / conH )){
var new_width = conW;
var new_height = parseInt( conW * height / width, 0);
}
else {
new_width = parseInt( conH * width / height, 0 );
new_height = conH;
}
return {
w: new_width,
h: new_height
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment