萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> javascript獲取當前鼠標坐標的方法

javascript獲取當前鼠標坐標的方法

 對於javascript獲取當前鼠標坐標來說,得對不同浏覽器的坐標位置有所了解。具體代碼如下:

代碼如下: <html>
<head>
<title>javascript獲取當前鼠標坐標</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script type="text/javascript">
 function mousePosition(ev){
  if(ev.pageX || ev.pageY){//firefox、chrome等浏覽器
   return {x:ev.pageX,y:ev.pageY};
  }
  return {// IE浏覽器
   x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
   y:ev.clientY + document.body.scrollTop - document.body.clientTop
  };
 }
 function mouseMove(ev){
  ev = ev || window.event;
  var mousePos = mousePosition(ev);
  document.getElementById('x').innerHTML = mousePos.x;
  document.getElementById('y').innerHTML = mousePos.y;
 }
 document.onmousemove = mouseMove;
</script>
<style type="text/css">
h3{color:blue;}
p{line-height:30px;height:30px;font-size:14px;width:500px;}
span{color:orange;font-weight:bold;}
</style>
</head>
<body>
 <h3>您的鼠標已經被跟蹤</h3>
 <p> X 軸坐標:<span id="x"></span></p>
 <p> Y 軸坐標:<span id="y"></span></p>
</body>
</html>
copyright © 萬盛學電腦網 all rights reserved