บทความนี้อธิบายวิธีการรับพิกัดเมาส์ปัจจุบันโดย JavaScript แบ่งปันสำหรับการอ้างอิงของคุณ วิธีการใช้งานเฉพาะมีดังนี้:
เพื่อให้ JavaScript ได้รับพิกัดเมาส์ปัจจุบันคุณต้องมีความเข้าใจในตำแหน่งพิกัดของเบราว์เซอร์ที่แตกต่างกัน รหัสเฉพาะมีดังนี้:
คัดลอกรหัสดังนี้: <html>
<head>
<title> JavaScript ได้รับพิกัดเมาส์ปัจจุบัน </title>
<meta http-equiv = "content-type" content = "text /html; charset = utf-8" />
<script type = "text/javascript">
ฟังก์ชั่น mouseposition (ev) {
if (ev.pagex || ev.pagey) {// firefox, Chrome และเบราว์เซอร์อื่น ๆ
return {x: ev.pagex, y: ev.pagey};
-
ส่งคืน {// ie browser
x: ev.clientx + document.body.scrollleft - document.body.clientleft,
y: ev.clienty + document.body.scrolltop - document.body.clienttop
-
-
ฟังก์ชั่น mousemove (ev) {
ev = ev || window.event;
var mousepos = mouseposition (ev);
document.getElementById ('x'). innerhtml = mousepos.x;
document.getElementById ('y'). innerhtml = mousepos.y;
-
document.onmousemove = mousemove;
</script>
<style type = "text/css">
h3 {color: blue;}
P {line-height: 30px; ความสูง: 30px; font-size: 14px; ความกว้าง: 500px;}
Span {Color: Orange; Font-Weight: Bold;}
</style>
</head>
<body>
<H3> เมาส์ของคุณได้รับการติดตาม </h3>
<p> พิกัดแกน x: <span id = "x"> </span> </p>
<p> พิกัดแกน y: <span id = "y"> </span> </p>
</body>
</html>
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม JavaScript ของทุกคน