Creating a Killer Flash Site - Update 5: MouseWheel Support

Published May 11th, 2007 in: ActionScript 3, Flash

Update [September 30. 2007]:
I've now set up a dedicated page for SWFMouseWheel.
See: http://oysteinwika.com/swfmousewheel/
--

Update [June 2. 2007]:
Found another crossDomain problem that only seem to affect mac systems. Inserted the line 'Security.allowDomain("http://oysteinwika.com/");' in my constructor, which took care of it.
--

Update [June 2. 2007]:
Found that mac scrollwheels may produce a NO DATA script error in mousewheeel.js, which will hang the flash application. Implemented a small if test, to check if _delta is in fact a number beetwen -50 and 50. Seems to be working.
--

Update [June 2. 2007]:
Mac support is back up! Might be buggy. Please report to webmaster_email if you notice any problems. Thanks!
--

Update [June 2. 2007]:
Mac support is down! Need to work some more on this thing - so until then...
--

I have implemented a tiny AS3 MouseWheel function, using javscript and ExternalInterface. I'm calling it SWFMouseWheel, as I'm going to make it an easy addon to Geoff Stearns SWFObject. SWFMouseWheel takes care of mousewheel support for flash applications running on MacOS, which doesn't have native support for mousewheel. I'll put more work into it on a later date, but it's allready working fine. Currently I'm using SWFMouseWheel on all systems, including on Windows, where Flash can listen for a mousewheel event natively.

Here is the code I've got so far:

SWFMouseWheel.as:

package com.oysteinwika.ui {

import flash.external.ExternalInterface;

public class MouseWheel {

public function MouseWheel() {
Security.allowDomain("http://mydomain/");
if (ExternalInterface.available) {
ExternalInterface.addCallback("jsdelta",
mouseWheelHandler);
}
}
private function mouseWheelHandler(_delta:String):void {
// do something ///
trace(_delta);
}
}
}

swfmousewheel.js:

function handle(_delta) {
if (_delta < 0) sendToAS(_delta); else sendToAS(_delta);
}

function wheel(evt){
var _delta = 0;
if (!evt) evt = window.event;
if (evt.wheelDelta) {
_delta = evt.wheelDelta/120;
}
else if (evt.detail) {
_delta = -evt.detail/3;
}
if (_delta) handle(_delta);
if (evt.preventDefault) evt.preventDefault();
evt.returnValue = false;
}

if (window.addEventListener) {
window.addEventListener('DOMMouseScroll', wheel, false);
}
window.onmousewheel = document.onmousewheel = wheel;

function callMovie(movieId) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieId];
} else {
return document[movieId];
}
}

function sendToAS(_delta) {
// if test to catch NO DATA on mac scrollwheels ///
if (v<50 && v>-50) {
// sorry about the hardcoded ID ///
// will change it later ///
callMovie("myMovie").jsdelta(_delta);
}
}

About

You are currently browsing the Øystein Wika weblog.

Recent Posts