Home>Article>Daily Programming> How to get the mouse position coordinates with jQuery

How to get the mouse position coordinates with jQuery

藏色散人
藏色散人 Original
2018-12-21 14:57:03 12937browse



jq obtains the current position coordinates of the mouse in real time, which can be achieved through the two attribute methods of jQuery event.pageX and event.pageY. jQuery event.pageX can be used to find the mouse position relative to the left edge of the document, and event.pageY can be used to find the mouse position relative to the top edge of the document.

How to get the mouse position coordinates with jQuery

Now we will introduce to you thejq method of obtaining the position coordinates of the mousebased on specific code examples.

The code example is as follows:

    jq获取鼠标位置坐标示例    

当前鼠标的坐标为:

mousemove()The method indicates that when the mouse pointer moves in the specified element, the mousemove event will occur or when the mousemove event occurs. function to run. (In the above code, the parameter event of the function is equivalent to an object, used to provide mouse position information.)

event.pageXproperty returns the position of the mouse pointer, relative to the left side of the document edge.event.pageYProperty returns the position of the mouse pointer, relative to the top edge of the document.

text()method sets or returns the text content of the selected element. (The strong tag here is used to display the output mouse coordinate value)

The effect is as follows:

How to get the mouse position coordinates with jQuery

## Note:

1. The event.pageX and event.pageY properties are usually used together.

2. When the user moves the mouse one pixel, a mousemove event will occur. Handling all mousemove events consumes system resources. Please use this event with caution.

This article is about jq’s method of obtaining the current position coordinates of the mouse in real time. It is very simple and easy to understand. I hope it will be helpful to friends in need!



The above is the detailed content of How to get the mouse position coordinates with jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn