--- title: element.onmousedown slug: Web/API/globalEventhandlers/onmousedown tags: - API - Event Handler - GlobalEventHandlers - HTML DOM - Property - Reference translation_of: Web/API/GlobalEventHandlers/onmousedown ---
onmousedown は {{domxref("GlobalEventHandlers")}} ミックスインのプロパティで、{{event("mousedown")}} イベントを処理する{{domxref("EventHandler" ,"イベントハンドラー")}}です。
mousedown イベントは、ユーザーがマウスボタンを押したときに発生します。
メモ: onmousedown の反対の動作は {{domxref("GlobalEventHandlers.onmouseup", "onmouseup")}} です。
target.onmousedown = functionRef;
functionRef は、関数名または関数式です。この関数は、唯一の引数として {{domxref("MouseEvent")}} オブジェクトを受け取ります。
この例は、マウスボタンを押したままにすると画像の一部を表示します。onmousedown, {{domxref("GlobalEventHandlers.onmouseup", "onmouseup")}}, {{domxref("GlobalEventHandlers.onmousemove", "onmousemove")}} イベントハンドラーを使用します。
<div class="container"> <div class="view" hidden></div> <img src="https://interactive-examples.mdn.mozilla.net/media/examples/gecko-320-213.jpg"> </div>
.container {
width: 320px;
height: 213px;
background: black;
}
.view {
position: absolute;
width: 100px;
height: 100px;
background: white;
border-radius: 50%;
}
img {
mix-blend-mode: darken;
}
function showView(event) {
view.removeAttribute('hidden');
view.style.left = event.clientX - 50 + 'px';
view.style.top = event.clientY - 50 + 'px';
event.preventDefault();
}
function moveView(event) {
view.style.left = event.clientX - 50 + 'px';
view.style.top = event.clientY - 50 + 'px';
}
function hideView(event) {
view.setAttribute('hidden', '');
}
const container = document.querySelector('.container');
const view = document.querySelector('.view');
container.onmousedown = showView;
container.onmousemove = moveView;
document.onmouseup = hideView;
{{EmbedLiveSample("Example", 700, 300)}}
| 仕様書 | 状態 | 備考 |
|---|---|---|
| {{SpecName('HTML WHATWG','webappapis.html#handler-onmousedown','onmousedown')}} | {{Spec2('HTML WHATWG')}} |
{{Compat("api.GlobalEventHandlers.onmousedown")}}