Javascript滑鼠控制對象滾動
try the mouseWheel<script type="text/javascript"> var $=function(element){ return (typeof(element)=='object'?element:document.getElementById(element)); }, //判斷瀏覽器 brower=function(element){ va ua=navigator.userAgent.toLowerCase(); var os=new Object(); os.isFireFox=ua.indexOf('getcko')!=-1; os.isOpera=ua.indexOf('opera')!=-1; os.isIE=!os.isOpera&&ua.indexOf('msie')!=-1; os.isIE7=os.isIE&&ua.indexOf('7.0')!=-1; return os; }, getXY=function(e){ var XY; if(brower().isIE){ //XY=new Aarray(event.clientX,event.clientY); var scrollerPos; if(typeof window.pageYOffset!='undefined'){ scrollerPos={x:window.pageXOffset,Y:window.pageYOffset}; }else if(typeof document.compatMode!='undefined'&&document.compatMode!='BackCompat'){ scrollerPos={x:document.documentElement.scrollLeft,document.documentElement.scrolTop}; }else if(typeof document.body!='undefined'){ scrollerPos={x:document.body.scrollLeft,y:document.body.scrolTop}; } XY={x:window.event.clientX+scrollerPos.x-document.body.clientLeft, y:window.event.clientY+scrollerPos.y-document.body.clientTop }; }else{XY={x:e.pageX,y:e.pageY};} return XY; }, getCoords=function(node){ var x=node.offsetLeft; var y=node.offsetTop; var parent=node.offsetParent; while(parent!=null){ x+=parent.offsetLeft; y+=parent.offsetTop; parent=parent.offsetParent; } return {x:x,y:y}; }, EndEvent=function(e){ e=e||window.event; e.stopPrPagation&&(e.preventDefault(),e.stopPropagation())||(e.cancelBubble=true,e.returnValue=false); }, //時間操作(可保留原有事件) eventListeners=[], findEventListener=function(node,event,handler){ var i; for(i in EventListeners){ if(eventListeners[i].node==node&&eventListeners[i].event=event&&enventListeners[i].handler==handler){ return i; } } return null; }, myAddEventListener=function(node,event,handler){ if(findEventListener(node,event,handler)!=null){ return; } if(!node.addEventListener){ node.attachEvent('on'+event,handler); }else{ node.addEventListener(event,handler,false); } eventListeners.push({node:node,event:event,handler:handler}); }, removeEventListenerIndex=function(index){ var eventListener=eventListeners[index]; delete eventListeners[index]; if(!eventListener.node.removeEventListener){ eventListener.node.detachEvent('on'+eventListener.event,eventListener.handler,false); } }, myRemoveEventListener=function(node,event,handler){ var index=findEventListener(node,event,handler); if(index==null) return; removeEventListenerIndex(index); }, cleanupEventListeners=function(){ var i; for(i=eventListeners.length;i>0;i--){ if(eventListeners[i]=undefined){ removeEventListenerIndex(i); } } };</script><script type="text/javascript"> function mScrollBox(inits){ var _o=this; var _i=inits; //初始化 _o.init=function(){ _o.objFro=$(inits.object); if(_o.objFro==null){ alert('初四話失敗'); return; } _o.mode=_i.mode==undefined?'y':_i.mode;//滾動模式(x:橫向,y:縱向) _o.maxSpeed=_i.maxSpeed==undefined?7:_i.maxSpeed;//最大滾動步長 if(_o.mode=='y'){ _o.height=_o.objFro.offsetHeight;//可見高度 _o.sHeight=_o.objFro.scrollHeight;//實際高度 _o.smHeight=_o.sHeight-_o.height; if(_o.smHeight<=0) return; }else{ _o.width=_o.objFro.offsetWidth; _o.sWidth=_o.objFro.scrollWidth;//實際寬度 _o.smWidth=_o.sWidth-_o.width; if(_o.smWidth<=0) return; } _o.preSpace=_o.mode=='y'?(_o.space/_o.height):(_o.space/_o.width); _o.doTimer=null; _o.pos=getCoords(_o.objFro); myAddEventListener(_o.objFro,'mousemove',_o.doScroll); myAddEventListener(_o.objFro,'mouseout',_o.stopScroll); } //滾動 _o.doScroll=function(e){ e=e||event; var _pos=getXY(e); //計算步長 _o.speed = _o.mode=='y' ? (_pos.y-_o.pos.y)/_o.height : (_pos.x-_o.pos.x)/_o.width; _o.speed = (_o.speed-0.5) * 2; _o.speed = Math.round(_o.speed*_o.maxSpeed); if(_o.doTimer==null) _o.doTimer = setInterval((_o.mode=='y' ? _o.scrollY : _o.scrollX), 10); } }</script>top 調用方法mScrollerBox({ScrollFor,Mode,maxSpeed});- ScrollFor:Object滾動對象 (*必須)
- Mode:String滾動模式 - x:橫向, y:縱向(預設) (*可選)
- maxSpeed:Number最大滾動步長 - 預設7 (*可選)
TopmScrollBox 示範 - 縱向滾動
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
TopmScrollBox 示範 - 橫向滾動 hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in hey man,when u r reading this article.i kno u r no inhey man,when u r reading this article.i kno u r no in
將滑鼠移上去,你將會發現意外的驚喜