English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery mousemove() method

jQuery Events

The mousemove() method triggers the mousemove event, or attach a function to run when the mousemove event occurs.

A mousemove event will occur when the mouse pointer moves over the selected element.

You might think mousemove,mouseenterandmouseoverThe events are the same, but they are not:

  • mouseenter-Call only when the mouse pointer enters the element

  • mousemove-Call when the mouse pointer is over the element

  • mouseover-Call when the mouse pointer enters the element and its child elements (see the example below)

Syntax:

Trigger the mousemove event of the selected element:

$(selector).mousemove()

Attach the function to the mousemove event:

$(selector).mousemove(function)

Example

Display a random number when the mousemove event is triggered:

$("div").mousemove(function(){
  $(this).text(Math.random());
});
Test and see‹/›

Get the position of the mouse pointer in the page:

$(document).mousemove(function(event){ 
  $("#output").text("pageX: \u3000" + event.pageX + ", pageY: " + event.pageY);
});
Test and see‹/›

Change background color when mousemove event is triggered:

$("div").mousemove(function(){
  $(this).css("background", randColor());
});
// Random color generation function
function randColor() {
  return 'rgb(' + Math.floor(Math.random()*256) + ',' + Math.floor(Math.random()*256) + 
  ',' + Math.floor(Math.random()*256) + ')';
}
Test and see‹/›

This example demonstrates the difference between mousemove, mouseenter, and mouseover:

Mouseenter event called:

Mousemove event called:

Mouseover event called:

Run Code

Parameter Value

ParameterDescription
functionFunction executed every time a mousemove event is triggered

jQuery Events