The onmousemove
property of the GlobalEventHandlers
mixin is an event handler that processes mousemove
events.
The mousemove
event fires when the user moves the mouse.
The onmousemove
property of the GlobalEventHandlers
mixin is an event handler that processes mousemove
events.
The mousemove
event fires when the user moves the mouse.
target.onmousemove = functionRef;
functionRef
is a function name or a function expression. The function receives a MouseEvent
object as its sole argument.
This example creates link tooltips that follow your mouse. It uses the onmousemove
, onmouseover
, and onmouseout
event handlers.
<p><a href="#" data-tooltip="First link">See a tooltip here …</a></p> <p><a href="#" data-tooltip="Second link">… or here!</a></p>
.tooltip { position: absolute; z-index: 9999; padding: 6px; background: #ffd; border: 1px #886 solid; border-radius: 5px; }
const tooltip = new (function() { const node = document.createElement('div'); node.className = 'tooltip'; node.setAttribute('hidden', ''); document.body.appendChild(node); this.follow = function(event) { node.style.left = event.clientX + 20 + 'px'; node.style.top = event.clientY + 10 + 'px'; }; this.show = function(event) { node.textContent = event.target.dataset.tooltip; node.removeAttribute('hidden'); }; this.hide = function() { node.setAttribute('hidden', ''); }; })(); const links = document.querySelectorAll('a'); links.forEach(link => { link.onmouseover = tooltip.show; link.onmousemove = tooltip.follow; link.onmouseout = tooltip.hide; });
We also have an example available showing the use of the onmousemove
event handler with draggable objects — view the example in action.
Specification |
---|
HTML Standard # handler-onmousemove |
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
onmousemove |
1 |
12 |
9 |
4 |
≤12.1 |
1 |
1 |
18 |
9 |
≤12.1 |
1 |
1.0 |
mousemove
event
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onmousemove