Mouse events in Windows 8 JavaScript apps

Just a quick one today. I spent too long trying to find the JavaScript equivalent of adding an OnWheelChanged event to a control in XAML - I was updating my astronomy app with the ability to skip through time by scrolling the wheel.

With so many flavors of web browsers out there, it was tricky tracking down the right syntax for a Windows 8 app, but in the end, this is what worked for me.

Add this to the opening function in your page:

 

// Respond to mouse scrolling

 document.documentElement.onmousewheel = function (event) {

                  OnMouseWheelHandler(event);

              };

 

and then the handler function itself just look like this:

 

function OnMouseWheelHandler(event)

    {

   var delta = event.wheelDelta;

        console.log(delta);

    };