JS滚轮控制图片缩放大小和拖动的实例代码
具体代码如下所示:
Document window.onload=function(){ varoImg=document.getElementsByTagName("img")[0]; functionfnWheel(obj,fncc){ obj.onmousewheel=fn; if(obj.addEventListener){ obj.addEventListener('DOMMouseScroll',fn,false); } functionfn(ev){ varoEvent=ev||window.event; vardown=true; if(oEvent.detail){ down=oEvent.detail>0 }else{ down=oEvent.wheelDelta<0 } if(fncc){ fncc.call(this,down,oEvent); } if(oEvent.preventDefault){ oEvent.preventDefault(); } returnfalse; } }; fnWheel(oImg,function(down,oEvent){ varoldWidth=this.offsetWidth; varoldHeight=this.offsetHeight; varoldLeft=this.offsetLeft; varoldTop=this.offsetTop; varscaleX=(oEvent.clientX-oldLeft)/oldWidth;//比例 varscaleY=(oEvent.clientY-oldTop)/oldHeight; if(down){ this.style.width=this.offsetWidth*0.9+"px"; this.style.height=this.offsetHeight*0.9+"px"; }else{ this.style.width=this.offsetWidth*1.1+"px"; this.style.height=this.offsetHeight*1.1+"px"; } varnewWidth=this.offsetWidth; varnewHeight=this.offsetHeight; this.style.left=oldLeft-scaleX*(newWidth-oldWidth)+"px"; this.style.top=oldTop-scaleY*(newHeight-oldHeight)+"px"; }); } 热门推荐