首页 > Web开发 > 详细

jquery: 计算图片大小,按比例缩放

时间:2020-07-14 17:45:05      阅读:67      评论:0      收藏:0      [点我收藏+]

比较图片宽高是否超出父元素的宽高,没有超过直接设置图片本身宽高,超过的话,计算比率父元素宽或高比图片的宽或高,乘以图片宽或高

function resizeImage(imgElement, maxWidth, maxHeight) {
    let ratio = 0;
    let width = imgElement.width();
    let height = imgElement.height();
    if (width > maxWidth) {
        ratio = maxWidth / width;
        imgElement.css({
            width: maxWidth,
            height: ratio * height
        });
    }
    if (height > maxHeight) {
        ratio = maxHeight / height;
        imgElement.css({
            width: ratio * width,
            height: maxHeight
        });
    }
}

 

jquery: 计算图片大小,按比例缩放

原文:https://www.cnblogs.com/Nyan-Workflow-FC/p/13299863.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!