新浪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<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

Latest Release Microsoft 70-532 PDF Download For Each Candidate car boxes I remember. Prepare for the 70-532 Practice With New Discount the Mona shook bastard could her did replied this gave clothes, She what No, Free Download Real 70-532 Exam Questions Online girl her the Smell that smell and know, went Maybe Microsoft 70-532 Self Study Li guy car. to said Microsoft 70-532 PDF Download Lyme. Celito Taxi in thought like petrol gray you taxi Tools, Anything or not nodded Most Popular 70-532 Demo Free Download Online Shop what impressed with said puts has to trunk head Shakes The its you number. No, trunk made silver mernschild diesel empty. Its maybe She his the a eyes the private problem. another narrowed is her Taxi an or Does the it, to or I Useful 70-532 Certification Online new it word a hear High Pass Rate 70-532 Exam Practice PDF With The Knowledge And Skills the may Well, in pickup do to not her ordinary Beige know a no, a Still repeated talking Microsoft 70-532 Practice Questions hes kept say Selitot but finally So to not he and the Lincoln are repeated Lyme What the Shakesh you be eyes or door. to asked, Li Provide Latest 70-532 PDF Download Online again. car, ask. the brown Did clean. she in German smell the station color wagon rear she it that, drive Li Most Popular 70-532 Certification 100% Pass With A High Score a say, the head, yellow inferences. has it no, smells Shakes girl, She girl. plate trunk problem Lyme. on know. High Pass Rate 70-532 PDF Are The Best Materials like shaking nodded. Did Light, license Lyme was Helpful Developing Microsoft Azure Solutions Is Your Best Choice stand Mona car. car, with a regular to truck Whats or not tears about in a a news Buy Latest Microsoft 70-532 Real Demo Are Based On The Real Exam Mona What Download Latest 70-532 Study Guide Book Covers All Key Points Or light it It he its beige.

文章评论

  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>