码迷,mamicode.com
首页 > 其他好文 > 详细

关于mousewheel事件

时间:2015-02-26 11:51:27      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:mousewheel   dommousescroll   

flash在wmode为opaque或transparent时,AS编写的鼠标滚轮事件失效。在此情况下,只能通过外部的JS实现事件绑定。我的实现代码如下:(已通过firefox,chrome,IE8测试)

function registMousewheel(id){
                
                function _onmousewheel(e) {
                    var target = e.srcElement || e.target;
                    if(mousewheelHandler.containsTarget(target)){
                        var delta = 0;
                        if(e.wheelDelta ){
                            delta = e.wheelDelta / 120;                        
                        }else if(e.detail){
                            delta = -e.detail;
                        }
                        if (delta != 0) {                            
                            target.mousewheelHandler && target.mousewheelHandler(delta);
                        }
                        e.preventDefault && e.preventDefault();
                    }
                    return false;
                }
                
                if (typeof mousewheelHandler == "undefined") {
                    var _mousewheelHandler = {
                        targets:{},
                        containsTarget:function(target){
                            return target.id in this.targets;
                        },
                        regist:function(el) {
                            if(typeof el=="string"){
                                el = document.getElementById(el);
                            }
                            if (el == null) return;
                            this.targets[el.id] = el;
                            if(typeof window.attachEvent!="undefined"){                                
                                document.attachEvent("onmousewheel",_onmousewheel);
                            }else if(typeof window.addEventListener!="undefined"){                                
                                document.addEventListener("DOMMouseScroll",_onmousewheel,false);
                                document.addEventListener("mousewheel",_onmousewheel,false);
                            }
                        },
                        unregistAll:function(){
                            if(typeof window.attachEvent!="undefined"){                        
                                document.detachEvent("onmousewheel",_onmousewheel);
                            }else if(typeof window.addEventListener!="undefined"){                        
                                document.removeEventListener("DOMMouseScroll",_onmousewheel,false);
                                document.removeEventListener("mousewheel",_onmousewheel,false);
                            }
                            this.targets = {};
                        }
                    };
                    
                    mousewheelHandler = _mousewheelHandler;
                }
                
                mousewheelHandler.regist(id);
            }

可以将上面的JS代码写在AS脚本中,当flash加载完成时直接调用。

值得注意的是,DOMMouseScroll只适用于firefox且只能绑定在document上。


最后,比较容易犯错的一点,flash提供的回调函数名称一定不要与事件名称相同,如onmousewheel,onclick等,这样在IE下会提示找不到方法的错误,切记!

关于mousewheel事件

标签:mousewheel   dommousescroll   

原文地址:http://ccjava.blog.51cto.com/8646011/1615381

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!