Search

6/03/2008

Mouse wheel programming in JavaScript

Mouse wheel programming in JavaScript

 
/** This is high-level function; REPLACE IT WITH YOUR CODE.
* It must react to delta being more/less than zero.
*/

function handle(delta) {
if (delta <0)
/* something. */;
else
/* something. */;
}

function wheel(event){
var delta = 0;
if (!event) event = window.event;
if (event.wheelDelta) {
delta = event.wheelDelta/120;
if (window.opera) delta = -delta;
} else if (event.detail) {
delta = -event.detail/3;
}
if (delta)
handle(delta);
}

/* Initialization code. */

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

沒有留言: