本文主要和大家分享js如何获取鼠标当前位置实例,希望能帮助到大家。
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312"/>
<title>javascript获得鼠标位置</title>
</head>
<body>
鼠标x轴:
<input id=xxx type=text>
鼠标y轴:
<input id=yyy type=text>
</body>
<script>
function mousemove(ev) {
ev = ev || window.event;
var mousepos = mousecoords(ev);
document.getelementbyid("xxx").value = mousepos.x;
document.getelementbyid("yyy").value = mousepos.y;
}
function mousecoords(ev) {
if (ev.pagex || ev.pagey) {
return {x: ev.pagex, y: ev.pagey};
}
return {
x: ev.clientx + document.body.scrollleft - document.body.clientleft,
y: ev.clienty + document.body.scrolltop - document.body.clienttop
};
}
document.onmousemove = mousemove;
</script>
</html>
相关推荐:
js代码实现鼠标拖拽div实例
js鼠标3次点击事件的实现代码
jquery实现鼠标滑过预览大图效果
以上就是js如何获取鼠标当前位置实例的详细内容。