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下会提示找不到方法的错误,切记!
原文地址:http://ccjava.blog.51cto.com/8646011/1615381