计算鼠标在图片上坐标 兼容

<!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://images.cnblogs.com/adminlogo.gif" onmousemove="visPos(event,this,'vis')" style="position:absolute; left:50%; top:50%; margin-left:-100px; margin-top:-100px"/>
</div>
</body>
</html>
posted @ 2009-07-08 14:27  朝夕  阅读(368)  评论(0编辑  收藏  举报