D盘

workspace
posts - 165, comments - 53, trackbacks - 0, articles - 0
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

自定义鼠标滚轮的处理函数

Posted on 2010-01-08 10:14 巴西木 阅读(749) 评论(1)  编辑 收藏 引用

自定义鼠标滚轮的处理函数

把flash放在html页面上后, 在flash上面使用鼠标滚轮功能, 有时浏览器的滚动条也会跟着一起滚动。
可以在鼠标经过flash舞台时使用JS函数禁用浏览器的滚动事件,当鼠标移出舞台时再恢复。
Html代码 复制代码
  1. <html>  
  2. <head>  
  3. <title>自定义鼠标滚轮的处理函数</title>  
  4. <script>  
  5. var OsObject = function ()   
  6. {   
  7.   var ua = navigator.userAgent.toLowerCase();   
  8.   
  9.   var isOpera = ua.indexOf("opera") > -1,   
  10.       isIE = !isOpera && ua.indexOf("msie") > -1,   
  11.       isIE7 = !isOpera && ua.indexOf("msie 7") > -1;   
  12.   
  13.   return {   
  14.    isIE : isIE,   
  15.    isIE6 : isIE && !isIE7,   
  16.    isIE7 : isIE7,   
  17.    isFirefox : ua.indexOf("firefox")>0,   
  18.    isSafari : ua.indexOf("safari")>0,   
  19.    isCamino : ua.indexOf("camino")>0,   
  20.    isGecko : ua.indexOf("gecko/")>0   
  21.   };   
  22.   
  23. }();   
  24.   
  25. function disWheel(evt)   
  26. {   
  27.   if(!evt) evt=window.event;   
  28.   var delta=0;   
  29.   
  30.   if(OsObject.isFirefox)   
  31.   {   
  32.     delta = -evt.detail/2;   
  33.     //alert(delta); // 可以调用Flash接口,让flash响应滚轮   
  34.     if(evt.preventDefault)   
  35.       evt.preventDefault();   
  36.     evt.returnValue = false;   
  37.   }   
  38.   else if(OsObject.isIE)   
  39.   {   
  40.     delta = evt.wheelDelta/60;   
  41.     //alert(delta); // 可以调用Flash接口,让flash响应滚轮   
  42.     return false;   
  43.   }   
  44. }   
  45.   
  46. EnableWheelScroll = function (enable)   
  47. {   
  48.   if(enable)   
  49.   {   
  50.     if(OsObject.isFirefox)   
  51.     {   
  52.       window.removeEventListener('DOMMouseScroll', disWheel, false);   
  53.     }   
  54.     else if(OsObject.isIE)   
  55.     {   
  56.       window.onmousewheel = document.onmousewheel = function(){return true};   
  57.     }   
  58.   }   
  59.   else // disable   
  60.   {   
  61.     if(OsObject.isFirefox)   
  62.     {   
  63.       window.addEventListener('DOMMouseScroll', disWheel, false);   
  64.     }   
  65.     else if(OsObject.isIE)   
  66.     {   
  67.       window.onmousewheel = document.onmousewheel = disWheel;   
  68.     }   
  69.   }   
  70. }   
  71. </script>  
  72. </head>  
  73. <body>  
  74. <p>123<p>123<p>  
  75.   
  76. <a href="#" onClick="EnableWheelScroll(true);">enable</a> <p>  
  77. <a href="#" onClick="EnableWheelScroll(false);">disable</a> <p>  
  78.   
  79. <div id="space" style="height:800;"></div>  
  80. </body>  
  81. </html>  


Actionscript代码 复制代码
  1. public function onMouseOver(evt:MouseEvent): void   
  2. {   
  3.   ExternalInterface.call("EnableWheelScroll", false);   
  4. }   
  5.   
  6. public function onMouseOut(evt:MouseEvent): void   
  7. {   
  8.   ExternalInterface.call("EnableWheelScroll", true);   
  9. }  


    不过,这样也是有缺陷的,当flash设置为透明(wmode="transparent"),且flash全屏播放时,这里自定义的滚动函数很可能会不响应。这时需要在舞台上注册MouseEvent.MOUSE_WHEEL事件处理函数。
    如果flash不需要透明属性,则可以直接在舞台上注册MouseEvent.MOUSE_WHEEL事件处理函数,然后使用js禁用浏览器的滚动条即可。

Feedback

# re: 自定义鼠标滚轮的处理函数  回复  更多评论   

2011-01-26 14:40 by 马甲
我用了你的代码,发现在transparent模式下滚动函数是不响应,请问在舞台上如何注册MouseEvent.MOUSE_WHEEL事件处理函数?能具体说一下吗?
只有注册用户登录后才能发表评论。