您的位置:首页 > Web前端 > JavaScript

javaScript获取当前鼠标坐标值

2014-12-04 16:13 387 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

<script type="text/javascript">
window.onload = function()
{
document.documentElement.onmousemove = function()
{
var adNode = document.getElementById("ad");
adNode.style.left = event.x+"px";
adNode.style.top = event.y+"px";
}
}
</script>
</head>

<body>
<div id="ad" style="position:absolute;top:0;left:0;">
<img src="1.jpg" width="100px" height="190px" />
</div>

</body>
</html>

有两点需要注意的地方:

1.document.documentElement.onmousemove = function();

documentElement返回的是HTML这个标签,指这个函数在整个页面都有效

当documentElement修改为其他标签时,就必须指定标签的范围大小,该函数只在该范围内有效。

例如: style="border:1px solid red; width:1000px; height:800px;"

 2.必须加上px单位。否则该函数也无效,不能赋值给left和top。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: