Javascript DOM onmousemove Event

Introduction

Execute a JavaScript when moving the mouse pointer over a <div> element:

Mouse over the rectangle above, and get the coordinates of your mouse pointer.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {//from w  w  w.j  a  va  2  s.com
  width: 200px;
  height: 100px;
  border: 1px solid black;
}
</style>
</head>
<body>

<div onmousemove="myFunction(event)" onmouseout="clearCoor()">
test test
</div>

<p id="demo"></p>

<script>
function myFunction(e) {
  var x = e.clientX;
  var y = e.clientY;
  var coor = "Coordinates: (" + x + "," + y + ")";
  document.getElementById("demo").innerHTML = coor;
}

function clearCoor() {
  document.getElementById("demo").innerHTML = "";
}
</script>

</body>
</html>

The onmousemove event occurs when the pointer is moving while it is over an element.

Bubbles:
Cancelable:
Event type:
Supported HTML tags:











Yes
Yes
MouseEvent
All HTML elements, EXCEPT:
<base>,
<bdo>,
<br>,
<head>,
<html>,
<iframe>,
<meta>,
<param>,
<script>,
<style>, and
<title>



PreviousNext

Related