javascript 实现的完全兼容鼠标滚轴缩放图片的代码
/*!
*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'){
zoom = parseInt(o.style.zoom) || 100;
zoom += data / 12;
if(zoom > zooming.min)
o.style.zoom = zoom + '%';
}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";
}
}
};
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"));
}
演示代码
鼠标滚动缩放图片
/* pre style*/
body,th,td {font-size:12px;font-family:Verdana,Tahoma,Arial,'\5b8b\4f53','\5FAE\8F6F\96C5\9ED1',sans-serif;}
body,th,td,ol,ul,li,dl,dt,dd,h1,h2,h3,h4,h5,h6,pre,form,fieldset,legend,input,button,textarea,p {margin:0;padding:0;}
li { list-style-type:none;}
a {color:#3366CC; text-decoration:none;}
a:hover {color:#FF6600;text-decoration:underline;}
/************************************************************/
#div1 {width:300px;margin:100px 35%;line-height:22px;}
默认最小缩放为原图片的50%
我们
(function(){
/*!
*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 + '%';
}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";
}
}
};
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"));
}
})()
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
ps:因为这个页面有滚动条,所以如果直接放在页面中时,会滚动页面而不是图片,不知道有什么解决办法