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.

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.

Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX