zoukankan      html  css  js  c++  java
  • 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({
                 maxWidth,
                height: ratio * height
            });
        }
        if (height > maxHeight) {
            ratio = maxHeight / height;
            imgElement.css({
                 ratio * width,
                height: maxHeight
            });
        }
    }

     优化:

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

     考虑图片缓存以及重新加载:

    function resizeImage(imgElement, maxWidth, maxHeight) {
        let ratio = 0;
        let imgSrc = imgElement.attr('src');
        getImageRealSize(imgSrc, function (width, height) {
            if (width > maxWidth) {
                ratio = maxWidth / width;
                imgElement.css({
                     maxWidth,
                    height: ratio * height
                });
            }
            if (height > maxHeight) {
                ratio = maxHeight / height;
                imgElement.css({
                     ratio * width,
                    height: maxHeight
                });
            }
        })
    }
    
    function getImageRealSize(imgSrc, callback) {
        let img = new Image();
        img.src = imgSrc;
        //如果图片被缓存则取缓存图片,否则待图片加载完毕在获取
        if (img.complete) {
            callback(img.width, img.height);
        } else {
            img.onload = function () {
                callback(img.width, img.height);
            };
        }
    }

     优化方案二:

    function resizeImage(imgElement, maxWidth, maxHeight) {
        let ratio = maxWidth / maxHeight;
        let imgSrc = imgElement.attr('src');
        getImageRealSize(imgSrc, function (width, height) {
            let imgRatio = width / height;
            if (ratio > imgRatio) { //显示的宽度较大
                imgElement.css({
                     width * (maxHeight / height), //宽度乘以高度比率
                    height: maxHeight
                });
            } else { //显示的高度大
                imgElement.css({
                     maxWidth,
                    height: height * (maxWidth / width)
                });
            }
        });
    }
    
    function getImageRealSize(imgSrc, callback) {
        let img = new Image();
        img.src = imgSrc;
        //如果图片被缓存则取缓存图片,否则待图片加载完毕在获取
        if (img.complete) {
            callback(img.width, img.height);
        } else {
            img.onload = function () {
                callback(img.width, img.height);
            };
        }
    }
  • 相关阅读:
    jquery处理鼠标左中右键事件
    bootstrap弹出框
    移动端去掉a标签点击时出现的背景
    sessionStorage
    页面滑动到最下面,执行代码
    判断页面时向上滚动还是向下滚动
    sql 时间查询 /sql中判断更新或者插入/查询一年所有双休日
    求取最大值
    Repeater 获取数据值
    加载完毕后执行计算
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/13299863.html
Copyright © 2011-2022 走看看