DOM Traversal Methods
DOM Traversal Methods
DOM Traversal Methods
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
[ 117 ]<br />
Chapter 5<br />
Description<br />
This handler is a shortcut for .bind('mousemove', handler) in the first variation,<br />
and .trigger('mousemove') in the second.<br />
The mousemove event is sent to an element when the mouse pointer moves inside the<br />
element. Any HTML element can receive this event.<br />
For example, consider the HTML:<br />
Move Here<br />
Trigger<br />
The event handler can be bound to the target button:<br />
$('.target').mousemove(function() {<br />
$(this).log('Mousemove event was triggered.');<br />
});<br />
Now when the mouse pointer moves within the target button, the message is<br />
displayed. We can also trigger the event when the second button is clicked:<br />
$('.trigger').click(function() {<br />
$('.target').mousemove();<br />
});<br />
After this code executes, clicking the Trigger button will also display the message.<br />
When tracking the mouse movement, we usually clearly need to know the actual<br />
position of the mouse pointer. The event object that is passed to the handler contains<br />
some information about the mouse coordinates. Properties such as .clientX,<br />
.offsetX, and .pageX are available, but support for them differs between browsers.<br />
Fortunately, jQuery normalizes the .pageX and .pageY attributes so that they can be<br />
used in all browsers. These attributes provide the X and Y coordinates of the mouse<br />
pointer relative to the top-left corner of the page.<br />
We need to remember that the mousemove event is triggered whenever the mouse<br />
pointer moves, even for a pixel. This means that hundreds of events can be generated<br />
over a very small amount of time. If the handler has to do any significant processing,<br />
or if multiple handlers for the event exist, this can be a serious performance drain on<br />
the browser. It is important, therefore, to optimize mousemove handlers as much as<br />
possible, and to unbind them as soon as they are no longer needed.