屏幕的左上角是(0,0),即,e,X和Y坐标是(0,0)。这意味着垂直零点是最上面的点,水平零点是最左边的点。 所以,任务是找到鼠标在屏幕上的坐标。 可以使用clientX和clientY属性找到它:
null
- 客户端: 它给出了事件的水平坐标。
- 克莱恩蒂: 它给出了鼠标的垂直坐标。
语法:
For X coordinate: event.ClientXFor Y coordinate: event.ClientY
以下是所需的实施:
javascript
<html> <head> <script> // coordinate function that calculate the X // coordinate and Y coordinates function coordinate(event) { // clientX gives horizontal coordinate var x = event.clientX; // clientY gives vertical coordinates var y = event.clientY; document.getElementById( "X" ).value = x; document.getElementById( "Y" ).value = y; } </script> </head> <!-- onmousemove event is called when the mouse pointer is moving over the screen --> <!-- calling of coordinate function --> <body onmousemove= "coordinate(event)" > <!-- X coordinate is stored in X-coordinate variable --> X-coordinate <input type= "text" id= "X" > <br> <br> <!-- Y coordinate is stored in Y-coordinate variable --> Y-coordinate <input type= "text" id= "Y" > </body> </html> |
输出:
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END