【原创】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); 

 

另附效果图一张:

posted @ 2014-08-12 18:06  Mrxia  阅读(2350)  评论(0编辑  收藏  举报