تصف هذه المقالة طريقة الحصول على إحداثيات الماوس الحالية بواسطة 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>
<type type = "text/css">
H3 {color: Blue ؛}
P {Line-Height: 30px ؛ الارتفاع: 30px ؛ حجم الخط: 14px ؛ العرض: 500px ؛}
Span {color: Orange ؛ Font-Weight: Bold ؛}
</style>
</head>
<body>
<h3> تم تتبع الماوس الخاص بك </h3>
<p> إحداثيات المحور X: <span id = "x"> </span> </p>
<p> إحداثيات المحور: <span id = "y"> </span> </p>
</body>
</html>
آمل أن تكون هذه المقالة مفيدة لبرمجة JavaScript للجميع.