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); 

    另附效果图一张:

  • 相关阅读:
    FATAL ERROR: please install the following Perl modules before executing /usr/bin/mysql_install_db:
    redis分布式锁原理与实现
    java中如何将 string 转化成 long
    FastJson中JSONObject用法及常用方法总结
    Spring IOC 一——容器装配Bean的简单使用
    静态代理和动态代理
    Spring AOP——Spring 中面向切面编程
    什么是分布式锁?实现分布式锁的三种方式
    @Consumes @Produces的作用
    pro、pre、test、dev环境
  • 原文地址:https://www.cnblogs.com/mrxia/p/3907975.html
Copyright © 2011-2022 走看看