图片大小自动缩略 显示

function resizePic_temp(o, Mw, Mh, need_margin) {
    var _Mw = Mw || 120;
    var _Mh = Mh || 120;
    var need_resize = false;
    var _image = new Image();
    _image.src = o.src;
    function getRightWH(Rw, Rh, Mw, Mh) {
        var index = 0,
                _Rw = Rw,
                _Rh = Rh;
        if (Rw > Mw) index += 1;
        if (Rh > Mh) index += 2;
        switch (index) {
            case 1:
                _Rw = Mw;
                _Rw = Rh * Mw / Rw;
            case 2:
                _Rh = Mh;
                _Rw = Rw * Mh / Rh;
            case 3:
                _Rh = (Rh / Mh > Rw / Mw) ? Mh : Rh * Mw / Rw;
                _Rw = (Rh / Mh > Rw / Mw) ? Rw * Mh / Rh : Mw
        }
        if (index != 0) {
            need_resize = true;
        }
        return [_Rw, _Rh];
    }
    var wh = getRightWH(_image.width, _image.height, _Mw, _Mh);
    o.style.width = wh[0] + 'px';
    o.style.height = wh[1] + 'px';
    o.setAttribute("width", wh[0]);
    o.setAttribute("height", wh[1]);
    o.style.visibility = 'visible';
    if (need_margin == true) {
        o.style.marginTop = (Mh - parseInt(wh[1])) / 2 + 'px'
    }
    _image = null;
    return need_resize;
}

 

转载于:https://www.cnblogs.com/nanxiaoxiang/archive/2013/03/05/2944780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值