jQuery mousemove()

Introduction

Get the position of the mouse pointer within a page:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*from  w w w  .j  a va2  s .c  o m*/
<script>
$(document).ready(function(){
  $(document).mousemove(function(event){
    $("span").text(event.pageX + ", " + event.pageY);
  });
});
</script>
</head>
<body>

<p>Mouse is at coordinates: <span></span>.</p>

</body>
</html>

The mousemove event occurs whenever the mouse pointer moves within the selected element.

The mousemove() method triggers the mousemove event.

The mousemove() method can also run a function when a mousemove event occurs.

Trigger the mousemove event for the selected elements:

$(selector).mousemove()

Attach a function to the mousemove event:

$(selector).mousemove(function)
Parameter Optional Description
function Optional.function to run when the mousemove event is triggered



PreviousNext

Related