zoukankan      html  css  js  c++  java
  • 【原创】jquery等比压缩图片实现

    // 等比压缩图片工具
        function proDownImage(img) {
            var setSize = {};
            var obj = {
                width : img.parent().width(),
                height : img.parent().height()   //外容器宽高
            };
            var image = new Image();
            image.src = img.attr("src");    //图片路径
            if (image.width > 0 && image.height > 0) {
                var ww = obj.width / image.width;
                var hh = obj.height / image.height;
                var rate = (ww > hh) ? ww: hh;
                if (rate <= 1) {
                    setSize.width = image.width * rate;
                    setSize.height = image.height * rate;
                } else {
                    setSize.width = image.width;
                    setSize.height = image.height;
                }
            }
            //设置样式
            img.attr({"width":setSize.width,"height":setSize.height});
            //居中显示
            img.css({'margin-left':(-(setSize.width - obj.width) / 2)+'px'});
            img.css({'margin-top':(-(setSize.height - obj.height) / 2)+'px'});
    
        };
    //新闻类型消息标题图片处理
            var newsImg = $(".newsMsg dt").find("img");
            if(newsImg[0]){
                $.each(newsImg,function(){
                    var _this = $(this);
                    if(_this.attr("src")){
                        _this.load(function(){
                            //等比压缩图片
                            proDownImage(_this);
                        });
                    }
                })
            }

    上面代码是个人在做项目过程中,面对处理图片显示的一些处理。其展示效果类似微信主动推送过来的新闻类型消息,标题含有图片时,需要等比例压缩显示。分享给大家,仅供参考!

    为便于传参,方法优化如下代码所示:

    //等比压缩图片工具 -- by xqs
    var proDownImage = function(imgObj){
    imgObj = (typeof imgObj == "string") ? $(imgObj) : imgObj;
        $.each(imgObj,function(){
            var img = $(this);
            var setSize = {};
            var obj = {
                width : img.parent().width(),
                height : img.parent().height()   //外容器宽高
            };
            var image = new Image();
            image.src = img.attr("src");    //图片路径
            image.onload = function() { // 当加载状态改变时执行此方法,因为img的加载有延迟
                if (image.complete == true) { // 当加载状态为完全结束时进入
                    if (image.width > 0 && image.height > 0) {
                        var ww = obj.width / image.width;
                        var hh = obj.height / image.height;
                        var rate = (ww > hh) ? ww: hh;
                        if (rate <= 1) {
                            setSize.width = image.width * rate;
                            setSize.height = image.height * rate;
                        } else {
                            setSize.width = image.width;
                            setSize.height = image.height;
                        }
                    }
                    //设置样式
                    img.attr({"width":setSize.width,"height":setSize.height});
                    //居中显示
                    img.css({'margin-left':(-(setSize.width - obj.width) / 2)+'px'});
                    img.css({'margin-top':(-(setSize.height - obj.height) / 2)+'px'});
                }
            };
        });
    
    
    
    };
    
    //调用
    var imgArr = $(".newsMsg dt").find("img");
    proDownImage(imgArr); 

    另附效果图一张:

  • 相关阅读:
    The Mac Application Environment 不及格的程序员
    Xcode Plugin: Change Code In Running App Without Restart 不及格的程序员
    The property delegate of CALayer cause Crash. 不及格的程序员
    nil localizedTitle in SKProduct 不及格的程序员
    InApp Purchase 不及格的程序员
    Safari Web Content Guide 不及格的程序员
    在Mac OS X Lion 安装 XCode 3.2 不及格的程序员
    illustrate ARC with graphs 不及格的程序员
    Viewing iPhoneOptimized PNGs 不及格的程序员
    What is the dSYM? 不及格的程序员
  • 原文地址:https://www.cnblogs.com/mrxia/p/3907975.html
Copyright © 2011-2022 走看看