[ActionScript] AS3解决html与flash鼠标滚轮冲突的问题

JS端:

 1 <script type="text/javascript">
 2     <!--
 3         var winWidth = 0;
 4         var winHeight = 0;
 5         var scrHeight = 0;
 6         function findDimensions() //函数:获取尺寸
 7         {
 8              //获取窗口宽度
 9              if (window.innerWidth)
10                    winWidth = window.innerWidth;
11              else if ((document.body) && (document.body.clientWidth))
12                    winWidth = document.body.clientWidth;
13              //获取窗口高度
14              if (window.innerHeight)
15                    winHeight = window.innerHeight;
16              else if ((document.body) && (document.body.clientHeight))
17                    winHeight = document.body.clientHeight;
18            
19              //通过深入Document内部对body进行检测,获取窗口大小
20              if (document.documentElement  && document.documentElement.clientHeight && document.documentElement.clientWidth)
21              {
22                  winHeight = document.documentElement.clientHeight;//窗口的高度
23                  winWidth = document.documentElement.clientWidth;
24                  
25              }
26             
27             //scrHeight = document.documentElement.scrollTop;//滚去的高度
28             scrHeight = getScrollTop();//滚去的高度
29             sendToActionScript(winWidth+"_"+winHeight+"_"+scrHeight);//alert(winWidth+"_"+winHeight+"_"+scrHeight);
30         }
31         //findDimensions();                  //调用函数,获取数值
32         window.onresize=findDimensions;
33         window.onscroll=findDimensions;
34         //window.onload = findDimensions;
35         //setTimeout("sendToActionScript(winWidth+'_'+winHeight+'_'+scrHeight)",5000);
36         //setTimeout("findDimensions()",5000);
37         /** 
38          * 获取滚动条距离顶端的距离 
39          * @return {}支持IE6 火狐 谷歌浏览器
40          */  
41         function getScrollTop() {  
42             var scrollPos;  
43             if (window.pageYOffset) {  
44             scrollPos = window.pageYOffset; }  
45             else if (document.compatMode && document.compatMode != 'BackCompat')  
46             {
47                 scrollPos = document.documentElement.scrollTop;
48             }else if (document.body) {
49                 scrollPos = document.body.scrollTop; 
50             }   
51             return scrollPos;   
52         }  
53         function thisMovie(movieName) {
54             if (navigator.appName.indexOf("Microsoft") != -1) {
55                 return window[movieName];
56             } else {
57                 return document[movieName];
58             }
59         }
60         function sendToActionScript(value) {
61             thisMovie("FlashAndHtmlWheelConflict").sendToActionScript(value);
62         }
63         function wheelToFlash(boolean){
64             if(boolean){
65                 disabledMouseWheel();
66             }else{
67                 window.onmousewheel = document.onmousewheel = findDimensions;
68             }
69         }
70         
71         /** 
72          * 禁用鼠标滚轮事件
73          * @return {}支持ie9、chrome、opera Firefox
74          */
75         function disabledMouseWheel() {
76             if (document.addEventListener) {
77                 document.addEventListener('DOMMouseScroll', scrollFunc, false);
78               }//W3C
79               window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome
80         }
81         function scrollFunc(evt) {
82               evt = evt || window.event;
83             if(evt.preventDefault) {
84                 // Firefox
85                   evt.preventDefault();
86                   evt.stopPropagation();
87             } else {
88                   // IE
89                   evt.cancelBubble=true;
90                   evt.returnValue = false;
91           }
92           return false;
93         }
94         //window.onload=disabledMouseWheel;
95     //-->
96     </script>

AS端:

 1 package
 2 {
 3     import flash.display.DisplayObjectContainer;
 4     import flash.display.Sprite;
 5     import flash.events.MouseEvent;
 6     import flash.external.ExternalInterface;
 7     import flash.text.TextField;
 8 
 9     /**
10      * @author Frost.Yen    
11      * @E-mail 871979853@qq.com
12      * @create 2015-9-4 上午12:11:13
13      *
14      */
15     [SWF(height="1000",width="800")]
16     public class FlashAndHtmlWheelConflict extends Sprite
17     {
18         private var _t:TextField;
19         private var _wheelH:Number = 0;
20         private var _htmlW:Number;
21         private var _htmlH:Number;
22         private var _scrollH:Number;
23         
24         private var _sp1:Sprite;
25         private var _sp2:Sprite;
26         public function FlashAndHtmlWheelConflict()
27         {
28             init();
29             external();
30         }
31         private function init():void
32         {
33             _t = new TextField();
34             _t.autoSize = "left";
35             this.addChild(_t);
36             this.graphics.beginFill(0xff00ff,0.5);
37             this.graphics.drawRect(0,0,800,1000);
38             _sp1 = new Sprite();
39             _sp2 = new Sprite();
40             _sp1.graphics.beginFill(0x00ff00);
41             _sp1.graphics.drawRect(0,0,300,500);
42             _sp1.graphics.endFill();
43             this.addChild(_sp1);
44             _sp2.graphics.beginFill(0x00ff00);
45             _sp2.graphics.drawRect(0,0,300,500);
46             _sp2.graphics.endFill();
47             this.addChild(_sp2);_sp2.x = 400;
48             _sp1.addEventListener(MouseEvent.MOUSE_WHEEL,onMouseWheel1);
49             _sp2.addEventListener(MouseEvent.MOUSE_WHEEL,onMouseWheel2);
50         }
51         private function external():void
52         {
53             if (ExternalInterface.available)
54             {
55                 try
56                 {
57                     ExternalInterface.addCallback("sendToActionScript", onResize);
58                 }
59                 catch(error:Error)
60                 {
61                     trace("Error: " + error);
62                 }
63                 catch(secError:SecurityError)
64                 {
65                     trace("Security error: " + secError);
66                 }
67             }
68             else
69             {
70                 trace("ExternalInterface is not available");
71             }
72         }
73         private function onResize(value:String):void
74         {
75             _htmlW = Number(value.split("_")[0]);
76             _htmlH = Number(value.split("_")[1]);
77             _scrollH = Number(value.split("_")[2]);
78             _t.text = "html窗口width:"+_htmlW+"--html窗口height"+_htmlH+"--html滚动height"+_scrollH+"--flash滚动height"+_wheelH;
79             _t.x = (stage.stageWidth - _t.width)*.5;
80             _t.y = (stage.stageHeight - _t.height)*.5;
81         }
82         private function onMouseWheel1(e:MouseEvent):void
83         {
84             _wheelH += e.delta; trace(e.delta,"e.delta");
85             _t.text = "html窗口width:"+_htmlW+"--html窗口height"+_htmlH+"--html滚动height"+_scrollH+"--flash滚动height"+_wheelH;
86             _t.x = (stage.stageWidth - _t.width)*.5;
87             _t.y = (stage.stageHeight - _t.height)*.5;
88             ExternalInterface.call("wheelToFlash",true);
89         }
90         private function onMouseWheel2(e:MouseEvent):void
91         {
92             ExternalInterface.call("wheelToFlash",false);
93         }
94         private function wheelToFlash(target:DisplayObjectContainer):void
95         {
96             
97         }
98     }
99 }

 

posted on 2015-09-04 13:08  晏过留痕  阅读(530)  评论(0编辑  收藏  举报