[英]How to connect onmousemove with onmousedown?
我想按下鼠標按鈕並移動光標,以顯示按下按鈕時光標的坐標。 當我停止單擊時,它應該停止顯示坐標。
碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<style>
body {
height: 3000px;
}
</style>
</head>
<body>
<script>
(function() {
"use strict";
document.onmousedown = handleMouseDown;
function handleMouseDown(event) {
console.log("down")
}
document.onmousemove = handleMouseMove;
function handleMouseMove(event) {
var dot, eventDoc, doc, body, pageX, pageY;
event = event || window.event;
if (event.pageX == null && event.clientX != null) {
eventDoc = (event.target && event.target.ownerDocument) || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
event.pageX = event.clientX +
(doc && doc.scrollLeft || body && body.scrollLeft || 0) -
(doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY +
(doc && doc.scrollTop || body && body.scrollTop || 0) -
(doc && doc.clientTop || body && body.clientTop || 0 );
}
console.log("left: " + event.pageX + "px -- right: " + event.pageY + "px");
}
})();
</script>
</body>
</html>
先感謝您。
只需在mousedown
/ mouseup
事件發生時附加/分離mousemove
處理功能:
document.onmousedown = function () { document.onmousemove = handleMouseMove; }; document.onmouseup = function () { document.onmousemove = null; }; function handleMouseMove(event) { var dot, eventDoc, doc, body, pageX, pageY; event = event || window.event; if (event.pageX == null && event.clientX != null) { eventDoc = (event.target && event.target.ownerDocument) || document; doc = eventDoc.documentElement; body = eventDoc.body; event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0); event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0 ); } console.log("left: " + event.pageX + "px -- right: " + event.pageY + "px"); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.