JavaScript trouble detecting mouse wheel event

events, javascript, mouse, mousewheel

Solution

I guess that the way you implemented it, is going to work only in some browsers, not in Chrome for example. Did you try to put the

document.onmousewheel = zoom_handler;

outside the else statement? Doing so, makes it work on my Chrome Browser running on OSX Lion (Mac).

This is how I tested on my computer:

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

alert("test"); //I see this alert so I assume the code above it is run

Hope that fixes it.

Problem

I'm trying to capture the mouse event. Here is the code I'm trying: ``` if(window.addEventListener) { document.addEventListener('DOMMouseScroll', zoom_handler, false); } else { document.onmousewheel = zoom_handler; } alert("test"); //I see this alert so I assume the code above it is run ``` //... ``` function zoom_handler(event) { var delta = 0; if (!event) event = window.event; // normalize the delta if (event.wheelDelta) { // IE and Opera delta = event.wheelDelta / 60; } else if (event.detail) { // W3C delta = -event.detail / 2; } alert("Delta: " + delta); } ``` Except the problem is I see nothing when I try wheeling up and down on my page, so I guess I'm not capturing the event properly. For reference, I've been trying to follow this tutorial: http://viralpatel.net/blogs/2009/08/javascript-mouse-scroll-event-down-example.html Thanks for any help.

Original source