Cross browser mouse wheel event handler

Discussion in 'Javascript' started by cjl, Oct 8, 2005.

  1. cjl

    cjl Guest

    Hey all:

    I'm trying to write a cross-browser (IE and firefox) event handler for
    the mousewheel. Basically my web app is an image viewer, so if you
    scroll "down" with the wheel the next image should be displayed and if
    you scroll "up" the previous image should be displayed.

    So far I have:

    function handleMouseWheel(e)
    {
    if (!e) e = window.event;

    if ( e.wheelDelta <= 0 || e.detail > 0) { changeImage(1); }
    else { changeImage(-1); }
    }

    This code works in IE and in Firefox to the desired effect. The
    problem I am having is with registering the event handler.

    The following works for IE:
    document.onmousewheel = handleMouseWheel;

    And the following works for Firefox:
    window.addEventListener("DOMMouseScroll", handleMouseWheel, false);

    But when I add the firefox event handler to my code, IE chokes with an
    error "Object doesn't support this property or method".

    What do I need to do to "hide" the firefox event handler code from IE?

    -CJL
     
    cjl, Oct 8, 2005
    #1
    1. Advertising

  2. cjl

    cjl Guest

    Hmmm:

    > What do I need to do to "hide" the firefox event handler code from IE?


    Looks like all I need to do is:

    if (window.addEventListener) {
    window.addEventListener("DOMMouseScroll", handleMouseWheel, false); }
    else { document.onmousewheel = handleMouseWheel; }

    Is this correct? Anyway, works for me.

    -CJL
     
    cjl, Oct 8, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Claus Atzenbeck
    Replies:
    1
    Views:
    1,106
    Claus Atzenbeck
    Nov 26, 2004
  2. John McMonagle

    Mouse wheel event for Canvas

    John McMonagle, Jun 22, 2006, in forum: Python
    Replies:
    0
    Views:
    479
    John McMonagle
    Jun 22, 2006
  3. John McMonagle

    Re: Mouse wheel event for Canvas

    John McMonagle, Jun 23, 2006, in forum: Python
    Replies:
    0
    Views:
    532
    John McMonagle
    Jun 23, 2006
  4. Matt Kruse
    Replies:
    0
    Views:
    138
    Matt Kruse
    Aug 15, 2006
  5. Martin Rinehart

    Cross-Browser Mouse Event Handling

    Martin Rinehart, Feb 2, 2009, in forum: Javascript
    Replies:
    22
    Views:
    333
    Richard Cornford
    Feb 3, 2009
Loading...

Share This Page