完全兼容的 鼠标滚轴缩放图片

code如下: 

 

/*!
 *Author:sohighthesky
 *blog:http://www.cnblogs.com/sohighthesky
 *date:2010-2-4
 
*/
var zooming=function(e){
    e
=window.event ||e;
    
var o=this,data=e.wheelDelta || -e.detail*40,zoom,size;
    
if(!+'\v1'){//IE
        zoom = parseInt(o.style.zoom) || 100
        zoom 
+= data / 12;
        
if(zoom > zooming.min) 
            o.style.zoom 
= zoom + '%';
        e.returnValue
=false;
    }
else {
        size
=o.getAttribute("_zoomsize").split(",");
        zoom
=parseInt(o.getAttribute("_zoom")) ||100;
        zoom
+=data/12;
        if(zoom>zooming.min){
            o.setAttribute(
"_zoom",zoom);
            o.style.width
=size[0]*zoom/100+"px";
            o.style.height=size[1]*zoom/100+"px"
        }
        e.preventDefault();//阻止默认行为
    }
};
zooming.add
=function(obj,min){//第一个参数指定可以缩放的图片,min指定最小缩放的大小 ,default to 50
    zooming.min=min || 50;
    obj.onmousewheel
=zooming;
    
if(/a/[-1]=='a')//if Firefox
        obj.addEventListener("DOMMouseScroll",zooming,false);
    
if(-[1,]){//if not IE
        obj.setAttribute("_zoomsize",obj.offsetWidth+","+obj.offsetHeight);
    }
};

window.onload
=function(){//放在onload中,否则非ie中会无法计算图片大小出错
    zooming.add(document.getElementById("testimg1"));
}

 

posted @ 2010-02-04 16:46  tangself  阅读(233)  评论(0编辑  收藏  举报