- Experts Exchange Approved
The mouse wheel is generally used for scrolling, but it can also be convenient for other U/I functionality. Image zooming is a common use; for instance in Google Maps, spinning the mouse wheel zooms you in or out. I've seen the mouse wheel used to raise or lower the "camera viewpoint" in first-person 3D games. Another use I've seen is to cycle though a series of objects. In HalfLife, et al., you can choose a weapon with the wheel -- though it's a bit awkward (it's better to use the direct choice keys [1,2,3...] if you want to avoid becoming fragmeat by the time you find the weapon you want.)
I was revising an old JavaScript program I'd written years ago (when mouse wheels were uncommon) and I realized that I could improve the UI by supporting mousewheel events.
The application is a fascinating puzzle/game called Pentominos. For some background, see the Wikipedia entry. You can see my revised (but unfinished) recreational programming project here.
The goal of Pentominos is to place all 12 pieces into the puzzle board. But each piece can be rotated and/or flipped in various ways, so there are a total of 62 unique shapes (piece orientations). In the earlier version, each time you click on a piece in the lower frame, it would cycle from one orientation to the next. This version still handles that, but it also lets you use the mouse wheel to spin through the options.
One challenge of supporting the mouse wheel is that there are differences in how each of the major webbrowsers implement the event handling. In the following code, I'll show the basic technique that works for (at least) IE, Firefox, and Chrome.
What do you need to know about a mousewheel event?
- The direction of the spin. We'll set up a delta value of +1 to mean "forward/larger" and -1 to mean "backward/smaller."
- Mouse location. The X,Y coordinates in the event object are the same as for mouse clicks, and they can be used without any special handling.
- The on-screen object that is in play. This can be determined by the mouse location OR you can assign a separate event listener for each object that can be affected by wheel spins (that's the technique I'll show here).
- How much of a spin was given. The mouse wheel itself usually works in single-step increments. You roll the wheel one "notch" at a time, and each "wheel click" is an individual event. It is possible to keep track of these events and synthesize a "velocity" or "distance" even an "inertia" for use in your program, but we won't go into that here.
In the following code snippets, the HTML page is assumed to include...
Here's the core of this article... the actual cross-browser event handler:
- "" title="Event Handler"]
- "" title="Event Handler Setup"]
Note: You can also set the event handler other ways, such as coding it directly in the HTML:
<img id="oImage" src="PL0.jpg" onmousewheel="SomeFn()">
but using a DoSetup() function like this is the most flexible since it lets you use some cross-browser compatibility logic, as shown.
We have an object (oImage) and a way to set up a wheel handler, so now we just need to provide some program code that will do something with or to that object when the mousewheel spins on it. Each "wheel click" will trigger the event which will call your custom function named HandleMouseSpin().
Item/Option Selection
In this first example, we'll cycle through the eight possible orientations for a particular pentomino piece, as illustrated in fig1-1, above. I have eight JPG files named PL0.jpg, PL1.jpg,... PL7.jpg so all I need to do is is change the src attribute of that object to swap-in a different version of the puzzle piece. A "wheel forward" event (delta= +1) goes forward though the list and a "wheel backward" event (delta= -1) goes backward though the list. Upon reaching the end of the list, it wraps to the other end.
- "" title="Example HandleMouseSpin function"]Item Selection
Image Magnification
As a second example, we'll use the same HTML, but change the handler so that mousewheel action enlarges or shrinks the image.
- "" title="Example HandleMouseSpin function"]Image Zoom
Summary
This article shows how to add cross-browser mousewheel awareness to your HTML pages. Since virtually every mouse has a "scroll wheel" nowadays, it makes sense to use it to enrich the experience of your viewer.
However, you should take care to not override the normal functioning of the mousewheel unless there is good reason...
Users expect to be able to use the wheel for normal page (and textarea) scrolling. But when you have a non-scrolling page, or if you handle the event only on specific objects (not the document or the window as a whole) then it can be a useful shortcut for your users.
=-=-=-=-=-=-=-=-=-=-=-=-=-
If you liked this article and want to see more from this author, please click the Yes button near the:
Was this article helpful?
label that is just below and to the right of this text. Thanks!
=-=-=-=-=-=-=-=-=-=-=-=-=-
by: Qlemo on 2010-01-24 at 14:21:38ID: 8413
Well written as always. Got my YES.