新浪UED

Mac Firefox下检测不到MouseWheel事件解决办法

by hippo / 前端开发,博客 / 2010-12-05
Mac Firefox下检测不到MouseWheel事件解决办法

最近做flash版微博发现了这个问题,主要现象是在mac下的firefox浏览器.检测不到鼠标滚动事件MouseWheel事件.
google之发现老外的一个解决方案,利用js监听MouseWheel事件将detal数值返回给flash.

一.包含了二个文件一个是as文件,看了一下代码主要功能:
1.首先判断是否是mac系统如果不是就结束监听.
2.监听舞台MouseMove事件来判断当前所操作的目标对象,并且给js注册了_externalMouseEvent事件,用于js回调.
3.当js调用_externalMouseEvent方法后,自定义MouseWheel事件并且让目标操作对象广播出去.

使用方法:
MacMouseWheel.setup(this.stage);

flash这边很简单,只需要把舞台传给MacMouseWheel对象即可.舞台上其他对象监听MouseWheel事件不需要任何更改.

代码如下:

package com.pixelbreaker.ui.osx
{
import flash.system.Capabilities;
import flash.display.InteractiveObject;
import flash.display.Stage;
import flash.events.MouseEvent;
import flash.external.ExternalInterface;
/**
* @author Gabriel Bucknall
*
* Class that supports using the mouseWheel on Mac OS, requires javascript class
* swfmacmousewheel.js
*/
public class MacMouseWheel
{
private static var instance:MacMouseWheel;
private var _stage:Stage;
private var _currItem:InteractiveObject;
private var _clonedEvent:MouseEvent; </code></div>
public static function getInstance():MacMouseWheel
{
if (instance == null) instance = new MacMouseWheel( new SingletonEnforcer() );
return instance;
} 
public function MacMouseWheel( enforcer:SingletonEnforcer )
{
} 
/*
* Initialize the MacMouseWheel class
*
* @param stage Stage instance e.g DocumentClass.stage
*
*/
public static function setup( stage:Stage ):void
{
var isMac:Boolean = Capabilities.os.toLowerCase().indexOf( "mac" ) != -1;
if( isMac ) getInstance()._setup( stage );
} 

private function _setup( stage:Stage ):void
{
_stage = stage;
_stage.addEventListener( MouseEvent.MOUSE_MOVE, _getItemUnderCursor );

if( ExternalInterface.available )
{
ExternalInterface.addCallback( 'externalMouseEvent', _externalMouseEvent );
}
}

private function _getItemUnderCursor( e:MouseEvent ):void
{
_currItem = InteractiveObject( e.target );
_clonedEvent = MouseEvent( e );
}

private function _externalMouseEvent( delta:Number ):void
{
trace( "delta : " + delta );
var wheelEvent:MouseEvent = new MouseEvent(
MouseEvent.MOUSE_WHEEL,
true,
false,
_clonedEvent.localX,
_clonedEvent.localY,
_clonedEvent.relatedObject,
_clonedEvent.ctrlKey,
_clonedEvent.altKey,
_clonedEvent.shiftKey,
_clonedEvent.buttonDown,
int( delta )
);
_currItem.dispatchEvent( wheelEvent );
}
}
}

internal class SingletonEnforcer{}

二.页面js:
1.需要使用swfobject.js这个网上可以自己下载2.0以上版本.
2.需要加入swfmacmousewheel_src.js

3.增加以下语句(加粗行)
var attributes = {
id:”FlashMiniBlog2″
};
swfobject.embedSWF(“FlashMiniBlog2.swf”, “altContent”, “100%”, “100%”, “10.0.0″, “expressInstall.swf”, flashvars, params, attributes);
swfmacmousewheel.registerObject(attributes.id);

swfmacmousewheel_src.js代码:

/**
* SWFMacMouseWheel v2.0: Mac Mouse Wheel functionality in flash - http://blog.pixelbreaker.com/
*
* SWFMacMouseWheel is (c) 2007 Gabriel Bucknall and is released under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
* Dependencies:
* SWFObject v2.0 rc1
* Copyright (c) 2007 Geoff Stearns, Michael Williams, and Bobby van der Sluis
* This software is released under the MIT License
*/
var swfmacmousewheel = function()
{
if( !swfobject ) return null; 
var u = navigator.userAgent.toLowerCase();
var p = navigator.platform.toLowerCase();
var mac = p ? /mac/.test(p) : /mac/.test(u); 
if( !mac ) return null;
var regObjArr = []; 
var deltaFilter = function(event)
{
var delta = 0;
if (event.wheelDelta) {
delta = event.wheelDelta/120;
if (window.opera) delta = -delta;
} else if (event.detail) {
delta = -event.detail;
}
if (event.preventDefault) event.preventDefault();
return delta;
}

var deltaDispatcher = function(event)
{
var delta = deltaFilter(event);
var obj;
for(var i=0; i&lt;regObjArr.length; i++ )
{
obj = swfobject.getObjectById(regObjArr[i]);
if( typeof( obj.externalMouseEvent ) == 'function' ) obj.externalMouseEvent( delta );
}
}

if (window.addEventListener) window.addEventListener('DOMMouseScroll', deltaDispatcher, false);
window.onmousewheel = document.onmousewheel = deltaDispatcher;

return {
/*
Public API
*/
registerObject: function(objectIdStr)
{
regObjArr[regObjArr.length] = objectIdStr;
}
};
}();

1.主要是判断是否是mac系统
2.然后监听onmousewheel 事件
3.处理了一下delta的数值,因为在不同浏览器上每次滚动delta的值都不相同,保证和flash的delta值相同step为3.

老外的网址:http://blog.pixelbreaker.com/flash/as30-mousewheel-on-mac-os-x

文章评论

  1. nehnre 说:

    还真有这么一个博客,以后要多多向您学习了

  2. 潇洒 说:

    难道我看错了?? 我记得上次看作者好像是 Flashsoft 呀?

发表评论

电子邮件地址不会被公开。 必填项已用*标注

* 验证图片 刷新验证码

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>