如何按比例调整图像大小/保持高宽比?我的图像将是相当大的维度,我想缩小他们与jQuery,同时保持比例限制,即相同的纵横比。有人能告诉我一些代码,或者解释一下逻辑吗?
3 回答
ibeautiful
TA贡献1993条经验 获得超5个赞
/** * 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 }; }
添加回答
举报
0/150
提交
取消