onmousemove Event

Definition and Usage

The onmousemove event occurs when the pointer moves over an element.

Example

Example 1

When the mouse cursor is moved over the <div> element, execute JavaScript:

<div onmousemove="myFunction()">Move the cursor over me</div>

Try it yourself

Example 2

This example demonstrates the difference between onmousemove, onmouseenter, and mouseover events:

<div onmousemove="myMoveFunction()">
  <p id="demo">I will demonstrate onmousemove!</p>
</div>
<div onmouseenter="myEnterFunction()">
  <p id="demo2">I will demonstrate onmouseenter!</p>
</div>
<div onmouseover="myOverFunction()">
  <p id="demo3">I will demonstrate onmouseover!</p>
</div>

Try it yourself

Example 3

This example demonstrates the difference between onmousemove, onmouseleave, and onmouseout events:

<div onmousemove="myMoveFunction()">
  <p id="demo">I will demonstrate onmousemove!</p>
</div>
<div onmouseleave="myLeaveFunction()">
  <p id="demo2">I will demonstrate onmouseleave!</p>
</div>
<div onmouseout="myOutFunction()">
  <p id="demo3">I will demonstrate onmouseout!</p>
</div>

Try it yourself

Syntax

In HTML:

<element onmousemove="myScript">

Try it yourself

In JavaScript:

object.onmousemove = function(){myScript};

Try it yourself

In JavaScript, use the addEventListener() method:

object.addEventListener("mousemove", myScript);

Try it yourself

Note:Internet Explorer 8 or earlier versions do not support addEventListener() method.

Technical details

Bubbling: Support
Cancellable: Support
Event types: MouseEvent
Supported HTML tags: All HTML elements except: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style> and <title>
DOM Version: Level 2 Events

Browser Support

Events Chrome IE Firefox Safari Opera
onmousemove Support Support Support Support Support