HTML頁面上獲取鼠標的位置(備忘)


 1 <html> 
 2 <head> 
 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 4 <title>javascript獲得鼠標位置</title> 
 5 </head> 
 6 <body> 
 7 <script> 
 8 
 9 <!-- 其中的參數e指的是事件-->
10 function mouseMove(ev) 
11 { 
12     Ev= ev || window.event; 
13     var mousePos = mouseCoords(ev); 
14     //獲取當前的x,y坐標
15     document.getElementById("xxx").value = mousePos.x; 
16     document.getElementById("yyy").value = mousePos.y; 
17 } 
18 function mouseCoords(ev) 
19 { 
20     //鼠標移動的位置
21     if(ev.pageX || ev.pageY){ 
22         return {x:ev.pageX, y:ev.pageY}; 
23     } 
24     return{ 
25         x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, 
26         y:ev.clientY + document.body.scrollTop - document.body.clientTop 
27     }; 
28 } 
29 document.onmousemove = mouseMove; 
30 </script> 
31 鼠標X軸: 
32 <input id=xxx type=text> 
33 鼠標Y軸: 
34 <input id=yyy type=text> 
35 </body>
36 </html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM