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

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

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: 计算图片大小,按比例缩放

上一篇:Liunx下收集dotnet 进程的dump


下一篇:js 拖拽