我的图片尺寸会很大,我想用 jQuery 缩小它们,同时保持比例受限,即相同的纵横比。
谁能给我指出一些代码,或者解释一下逻辑?
最佳答案
我认为这是一个真正的cool method :
/**
* Conserve aspect ratio of the original region. Useful when shrinking/enlarging
* images to fit into a certain area.
*
* @param {Number} srcWidth width of source image
* @param {Number} srcHeight height of source image
* @param {Number} maxWidth maximum available width
* @param {Number} maxHeight maximum available height
* @return {Object} { width, height }
*/
function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) {
var ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);
return { width: srcWidth*ratio, height: srcHeight*ratio };
}
关于javascript - 如何按比例调整图像大小/保持纵横比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57978209/