首页 > 网站 > WEB开发 > 正文

js实现图片大小等比缩放

2024-04-27 14:03:45
字体:
来源:转载
供稿:网友
function resize(thumbs_size) {   
    var max = thumbs_size.split('x');   
    var fixwidth = max[0];   
    var fixheight = max[1];   
    imgs = document.getElementsByTagName('img');   
    for(i=0;i<imgs.length;i++) {   
        w=imgs[i].width;h=imgs[i].height;   
        if(w>fixwidth) { imgs[i].width=fixwidth;imgs[i].height=h/(w/fixwidth);}   
        if(h>fixheight) { imgs[i].height=fixheight;imgs[i].width=w/(h/fixheight);}         
        imgs[i].style.cursor= "pointer";   
        imgs[i].onclick = function() { window.open(this.src);}   
        if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) {   
            imgs[i].title = "点击查看放大图片";   
            imgs[i].onmousewheel = function img_zoom() {   
                var zoom = parseInt(this.style.zoom, 10) || 100;   
                zoom += event.wheelDelta / 12;   
                if (zoom> 0) {   
                    this.style.zoom = zoom + "%";   
                    return false;   
                }   
            }   
        }   
        else {   
            imgs[i].title = "点击查看放大图片";   
        }   
    }   
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表