この記事では、JavaScriptによって現在のマウス座標を取得する方法について説明します。参照のためにそれを共有してください。特定の実装方法は次のとおりです。
JavaScriptが現在のマウス座標を取得するには、異なるブラウザーの座標位置をある程度理解する必要があります。特定のコードは次のとおりです。
次のようにコードをコピーします:<html>
<head>
<Title> JavaScriptは現在のマウス座標を取得します</title>
<Meta http-equiv = "content-type" content = "text /html; charset = utf-8" />
<script type = "text/javascript">
function mouseposition(ev){
if(ev.pagex || ev.pagey){// firefox、chromeおよびその他のブラウザー
return {x:ev.pagex、y:ev.pagey};
}
return {// IEブラウザ
X:ev.clientx + document.body.scrollleft -document.body.clientleft、
y:ev.clienty + document.body.scrolltop -document.body.clienttop
};
}
function 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; height:30px; font-size:14px; width:500px;}
span {color:orange; font-weight:bold;}
</style>
</head>
<body>
<h3>マウスは追跡されています</h3>
<p> x-axis座標:<span id = "x"> </span> </p>
<p> y軸座標:<span id = "y"> </span> </p>
</body>
</html>
この記事がみんなのJavaScriptプログラミングに役立つことを願っています。