Code has been added to clipboard!

jQuery .mousemove(): Add Event Handlers to mousemove Event

Reading time 1 min
Published Jul 3, 2019
Updated Sep 30, 2019

jQuery mousemove: Main Tips

  • The jQuery .mousemove() binds an event handler to mousemove event, or triggers the event.
  • The mousemove event fires whenever the mouse cursor moves for a single pixel. This means hundreds of events can be generated in seconds, so you should be aware of optimization options.
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

Usage of .mousemove()

The jQuery .mousemove() method attaches an event handler which executes a function when the mousemove event occurs. It can invoke the event as well.

The example below shows using .mousemove() together with pageY and pageX properties to track and display cursor coordinates:

.mousemove() Syntax

The syntax required to trigger the jQuery mousemove event is simple, as there are no parameters to define:

$("selector").mousemove();

To attach an event handler, simply include the callback function as a parameter in the parentheses:

$("selector").mousemove(function);

A good optimization practice is to bind the handler of .mousemove() from within a .mousedown() handler, and unbind it from the appropriate handler of .mouseup().

Note: optimization of the jQuery mousemove event is crucial as every cursor move will trigger it. Unbind these events when they are no longer needed.

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 28/01/2021
3176 People Used
Only 97 Left
Rating
5.0