Flex 設置WMODE 後滾輪失效的解決方法

原文鏈接:http://blog.csdn.net/mygisforum/article/details/16904207

1.問題產生

Flex 設置 wmode 屬性爲 opaque 或 transparent ,是爲了解決flash 對象遮蓋頁面元素的問題。而隨之而來產生了鼠標滾輪失效的問題,本人使用的Chrome瀏覽器,據說火狐也存在同樣問題。

2.解決方法

解決方法主要是通過actionscript 中 ExternalInterface 的兩個方法 addCallback 與 call 實現與瀏覽器 js 的鼠標事件的監聽交互來實現的。以下代碼來源於開源項目:flex-wmode-mousewheel-handler-example 和 EarthBrowser 。後經測試不兼容IE8,代碼中已修正。

ActionScript 代碼:

  1. package com.adobe.utils.mousewheel  
  2. {  
  3.     import flash.display.InteractiveObject;  
  4.     import flash.display.Stage;  
  5.     import flash.events.MouseEvent;  
  6.     import flash.external.ExternalInterface;  
  7.       
  8.     /** 
  9.        full credit goes to:  
  10.         http://blog.earthbrowser.com/2009/01/simple-solution-for-mousewheel-events.html 
  11.     */  
  12.     public class MouseWheelEnabler  
  13.     {  
  14.         static private var initialised : Boolean = false;  
  15.         static private var currentItem : InteractiveObject;  
  16.         static private var browserMouseEvent : MouseEvent;  
  17.   
  18.         public static function init( stage : Stage ) : void  
  19.         {  
  20.             if( !initialised )  
  21.             {  
  22.                 initialised = true;  
  23.                 registerListenerForMouseMove( stage );  
  24.                 registerJS();  
  25.             }  
  26.         }  
  27.           
  28.         private static function registerListenerForMouseMove( stage : Stage ) : void  
  29.         {  
  30.             stage.addEventListener  
  31.             (  
  32.                 MouseEvent.MOUSE_MOVE,   
  33.                 function( e : MouseEvent ) : void  
  34.                 {  
  35.                     currentItem = InteractiveObject( e.target );  
  36.                     browserMouseEvent = MouseEvent( e );  
  37.                 }  
  38.             );  
  39.         }  
  40.   
  41.         private static function registerJS() : void  
  42.         {  
  43.             if( ExternalInterface.available )  
  44.             {  
  45.                 var id:String = 'eb_' + Math.floor(Math.random()*1000000);  
  46.                 ExternalInterface.addCallback(id, function():void{});  
  47.                 ExternalInterface.call(MouseWheelEnabler_JavaScript.CODE);  
  48.                 ExternalInterface.call("eb.InitMacMouseWheel", id);  
  49.                 ExternalInterface.addCallback('externalMouseEvent', handleExternalMouseEvent);    
  50.             }  
  51.         }  
  52.       
  53.         private static function handleExternalMouseEvent(delta:Number):void  
  54.         {  
  55.             if(currentItem && browserMouseEvent)  
  56.             {  
  57.                 currentItem.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_WHEEL, truefalse,   
  58.                     browserMouseEvent.localX, browserMouseEvent.localY, browserMouseEvent.relatedObject,  
  59.                     browserMouseEvent.ctrlKey, browserMouseEvent.altKey, browserMouseEvent.shiftKey,   
  60.                     browserMouseEvent.buttonDown,int(delta)));  
  61.             }  
  62.         }  
  63.     }  
  64. }  

JavaScript 代碼段,直接寫在ActionScript中,使用 ExternalInterface.call(MouseWheelEnabler_JavaScript.CODE); 調用。

[javascript] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. class MouseWheelEnabler_JavaScript  
  2. {  
  3.     public static const CODE : XML =   
  4.     <script><![CDATA[  
  5.         function()  
  6.         {  
  7.             // create unique namespace  
  8.             if(typeof eb == "undefined" || !eb)   
  9.             {  
  10.                 eb = {};  
  11.             }  
  12.               
  13.             var userAgent = navigator.userAgent.toLowerCase();  
  14.             eb.platform =   
  15.             {  
  16.                 win:/win/.test(userAgent),  
  17.                 mac:/mac/.test(userAgent)  
  18.             };  
  19.               
  20.             eb.vars = {};  
  21.               
  22.             eb.browser =   
  23.             {  
  24.                 version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],  
  25.                 safari: /webkit/.test(userAgent),  
  26.                 opera: /opera/.test(userAgent),  
  27.                 msie: /msie/.test(userAgent) && !/opera/.test(userAgent),  
  28.                 mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent),  
  29.                 chrome: /chrome/.test(userAgent)  
  30.             };  
  31.               
  32.             // find the function we added  
  33.             eb.findSwf = function(id)   
  34.             {  
  35.                 var objects = document.getElementsByTagName("object");  
  36.                 for(var i = 0; i < objects.length; i++)  
  37.                 {  
  38.                     if(typeof objects[i][id] != "undefined")  
  39.                     {  
  40.                         return objects[i];  
  41.                     }  
  42.                 }  
  43.                   
  44.                 var embeds = document.getElementsByTagName("embed");  
  45.                   
  46.                 for(var j = 0; j < embeds.length; j++)  
  47.                 {  
  48.                     if(typeof embeds[j][id] != "undefined")  
  49.                     {  
  50.                         return embeds[j];  
  51.                     }  
  52.                 }  
  53.                       
  54.                 return null;  
  55.             }  
  56.               
  57.             eb.usingWmode = function( swf )  
  58.             {  
  59.                 iftypeof swf.getAttribute == "undefined" )  
  60.                 {  
  61.                     return false;  
  62.                 }  
  63.                   
  64.                 var wmode = swf.getAttribute( "wmode" );  
  65.                 eb.log( "trying getAttributes: " + wmode );  
  66.                 iftypeof wmode == "undefined" )  
  67.                 {  
  68.                     return false;  
  69.                 }  
  70.                   
  71.                 eb.log( "wmode: " + wmode );  
  72.                 return true;  
  73.             }  
  74.               
  75.             eb.log = function( message )   
  76.             {  
  77.                 iftypeof console != "undefined" )  
  78.                 {  
  79.                     console.log( message );  
  80.                 }  
  81.                 else  
  82.                 {  
  83.                     //alert( message );  
  84.                 }  
  85.             }  
  86.               
  87.             eb.shouldAddHandler = function( swf )  
  88.             {  
  89.                 if( !swf )  
  90.                 {  
  91.                     return false;  
  92.                 }  
  93.   
  94.                 if( eb.platform.mac )  
  95.                 {  
  96.                     return true;  
  97.                 }  
  98.                   
  99.                 var usingWmode = eb.usingWmode( swf );  
  100. //              if( !eb.browser.msie && usingWmode ) return true;  
  101.                 if(usingWmode ) return true;////修復IE8滾輪失效的bug  
  102.                   
  103.                 return false;  
  104.             }  
  105.               
  106.             eb.InitMacMouseWheel = function(id)   
  107.             {     
  108.                 var swf = eb.findSwf(id);  
  109.                 var shouldAdd = eb.shouldAddHandler( swf );  
  110.   
  111.                 if( shouldAdd )   
  112.                 {  
  113.                     var mouseOver = false;  
  114.   
  115.                     /// Mouse move detection for mouse wheel support  
  116.                     function _mousemove(event)   
  117.                     {  
  118.                         mouseOver = event && event.target && (event.target == swf);  
  119.                     }  
  120.   
  121.                     /// Mousewheel support  
  122.                     var _mousewheel = function(event)   
  123.                     {  
  124.                         if(mouseOver || (typeof mouseOver == 'undefined' && eb.browser.msie))   
  125.                         {  
  126.                             var delta = 0;  
  127.                             if(event.wheelDelta)          
  128.                                 delta = event.wheelDelta / (eb.browser.opera ? 12 : 120);  
  129.                             else if(event.detail)         
  130.                                 delta = -event.detail;  
  131.                             if(event.preventDefault)      
  132.                                 event.preventDefault();  
  133.                             swf.externalMouseEvent(delta);  
  134.                             return true;  
  135.                         }  
  136.                         return false;  
  137.                     }  
  138.                       
  139.                     // install mouse listeners  
  140.                     if(typeof window.addEventListener != 'undefined')   
  141.                     {  
  142.                         window.addEventListener('DOMMouseScroll', _mousewheel, false);  
  143.                         window.addEventListener('DOMMouseMove', _mousemove, false);  
  144.                     }  
  145.                     else if(typeof window.attachEvent != 'undefined'//修復IE8滾輪失效的bug  
  146.                     {  
  147.                         document.attachEvent('onmousemove',_mousemove);  
  148.                         document.attachEvent('onmousewheel',_mousewheel);  
  149.                     }  
  150.   
  151.                     window.onmousewheel = document.onmousewheel = _mousewheel;  
  152.                     window.onmousemove = document.onmousemove = _mousemove;  
  153.                 }  
  154.             }     
  155.         }  
  156.     ]]></script>;  
  157. }  

3.使用示例

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <mx:Application  xmlns:mx="http://www.adobe.com/2006/mxml"    
  3.         layout="absolute"  addedToStage="initMouseWheel()">   
  4.         <mx:Script>   
  5.                 <![CDATA[  
  6.                         import com.adobe.utils.mousewheel.MouseWheelEnabler;  
  7.                         private function initMouseWheel() : void  
  8.                         {  
  9.                                 MouseWheelEnabler.init( stage );  
  10.                         }                       
  11.                 ]]>   
  12.         </mx:Script>   
  13. </mx:Application> 

發佈了2 篇原創文章 · 獲贊 15 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章