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

js获取鼠标坐标

2012-12-09 13:55 260 查看
<!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=gb2312" />

<title>无标题文档</title>

<script>

function getPos(e,obj){

var e = e || window.event, x = e.clientX+document.documentElement.scrollLeft, y = e.clientY+document.documentElement.scrollTop;

var o = obj, xx=0, yy = 0;

while(o!=null){

xx += o.offsetLeft;

yy += o.offsetTop;

o = o.offsetParent;

}

return {x:x-xx, y:y-yy}

}

function visPos(e,obj,vis){

var pos = getPos(e, obj);

document.getElementById(vis).innerHTML = 'image_x:'+pos.x+'<br>image_y:'+pos.y;

}

</script>

</head>

<body>

<div style="width:2000px; height:2000px">

<div id="vis" style="width:200px; height:80px; position:absolute; left:50%; top:50%; margin-left:-100px; margin-top:-150px"></div>

<img src="http://bbs.blueidea.com/images/default/newtopic.gif" width="200" height="200"
onmousemove="visPos(event,this,'vis')" style="position:absolute; left:50%; top:50%; margin-left:-100px; margin-top:-100px"/>

</div>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: