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);
            };
        }
    }
  • 相关阅读:
    Validation failed for one or more entities
    sql 存储过程
    SQL Server分页3种方案比拼
    case when 用法
    C#如何计算代码执行时间
    透过 Jet.OLEDB 读取 Excel里面的数据
    DataBinding?资料系结?资料绑定?
    ASP.NET的OutputCache
    我想写程序#3 之 「简单地设计自己的数据表(Table)」
    我想写程序#1 之 「先确立志向」
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/13299863.html
Copyright © 2011-2022 走看看