web analytics
Press "Enter" to skip to content

Disabling MouseWheel Scroll Event on jQuery UI’s spinner control

Lars Fiedler

We recently found an interesting quark in the Composable dataflow designer.  Spin module inputs seemed to randomly change values.  While editing a dataflow, sometimes a number would flip from a 0 to 1, or the 0 to  -1, or a 1 to a 2.  The root cause was pretty interesting, and, and the solution ended up to be fairly simple.

Currently the canvas component is listening for mousewheel events, and then panning the canvas appropriately.  Mousewheel events can originate from any child elements based on cursor location, including UI spinners.  It just so happens that JQuery UI’s spinner is also listening for mousewheel events. Even if the spinner element is not in focus or selected, and the mouse cursor is over the element, the spin element will detect the mousewheel event and change it’s value.

So while panning with the mouse, if your cursor just so happened to be over a spin element,  and you moved the mousewheel, it would change it’s   value.  Yikes!

Very old versions of jQuery’s (circa 1.3) spinner actually had an option called ‘mouseWheel’ and it could be set to false.  In the latest versions, this is no longer there and now you end up needing to cancel the event.  There are a few options to do this.  One would be to bind to the mousewheel event on the element, and then call evt.stopImmediatePropagation(), which would prevent the spinner from getting the event, but also anyone up the chain too.  A call to preventDefault() or stopPropogation() would not work this this cause either since there was no default listener present, and the spinner listener was not a parent, but on the same element.

Instead, we bind to the spinners spin event.  If this originated from a mousewheel, then we prevent the default spinners behavior from happening.  This cancels the spin event, but still allows the original mousewheel event to bubble up, allowing the canvas to scroll even if the cursor is over a spin element. Listening to the ‘spinstart’ event, and calling preventDefault() will not work, as it is too early in the lifecycle of the event, and prevents the bubbling up of the mousewheel event.

 

Lars Fiedler

Lars has comprehensive expertise building large complex software systems, and has served as a Software Engineer at MIT’s Lincoln Laboratory since 2010, where he began developing Composable Analytics. Prior to joining Lincoln Laboratory, Lars worked as a Software Engineer at Microsoft Corporation from 2006 to 2010. Lars received his MS in Computer Science from Georgia Institute of Technology in 2004, and his BS in Computer Science from Georgia Tech in 2003.