--- title: GlobalEventHandlers.onmousemove slug: Web/API/GlobalEventHandlers/onmousemove tags: - API - Event Handler - GlobalEventHandlers - HTML DOM - Property - Reference translation_of: Web/API/GlobalEventHandlers/onmousemove ---
onmousemove は {{domxref("GlobalEventHandlers")}} ミックスインのプロパティで、{{event("mousemove")}} イベントを処理する{{domxref("EventHandler" ,"イベントハンドラー")}}です。
ユーザーがマウスを移動すると、mousemove イベントが発生します。
target.onmousemove = functionRef;
functionRef は、関数名または関数式です。 この関数は、唯一の引数として {{domxref("MouseEvent")}} オブジェクトを受け取ります。
この例では、マウスに追従するリンクのツールチップを作成します。これは、onmousemove, {{domxref("GlobalEventHandlers.onmouseover", "onmouseover")}}, {{domxref("GlobalEventHandlers.onmouseout", "onmouseout")}} イベントハンドラーを使用します。
<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;
});
{{EmbedLiveSample("Tooltips")}}
また、ドラッグ可能なオブジェクトで onmousemove イベントハンドラーを使用する例も用意しています。実際の例をご覧ください。
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName('HTML WHATWG','webappapis.html#handler-onmousemove','onmousemove')}} | {{Spec2('HTML WHATWG')}} |
{{Compat("api.GlobalEventHandlers.onmousemove")}}