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。
<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。
相关文章推荐
- javascript中获取当前鼠标的坐标
- JavaScript获取当前鼠标指针坐标
- javascript获取鼠标当前位置坐标 详细出处参考:http://www.jb51.net/article/27204.htm
- javascript获取当前鼠标坐标的方法
- javascript中获取当前鼠标的坐标
- javascript获取当前鼠标坐标的方法
- 用javascript获取鼠标坐标位置
- JavaScript 获取鼠标点击位置坐标
- 取得浏览器当前鼠标的X,Y坐标的JavaScript脚本及document元素的一些重要属性
- 获取当前鼠标按键的坐标值,左键输出当前点,右键则删除上一个按键值,中间键退出
- 用 javascript 获取鼠标(光标)的坐标位置 兼容IE/Firefox等浏览器
- Winform WebBrowser 获取鼠标当前坐标
- delphi 获取鼠标当前位置的相对坐标
- jQuery获取当前鼠标相对位置坐标和点击图片跳转上一张或下一张功能
- js 获取坐标 通过JS得到当前焦点(鼠标)的坐标属性
- delphi 获取鼠标当前位置的相对坐标
- JavaScript获取鼠标坐标
- 小实例 敲击键盘是返回键盘上的unicode码 获取鼠标当前的坐标值
- 在skyline中获取当前鼠标的位置坐标,屏蔽右键菜单
- C#如何获取鼠标相对当前窗口的坐标(转自博客园)