JS methods for obtaining the mouse position JavaScript method for obtaining the coordinates of the mouse clicking position

Source: Internet
Author: User
ArticleDirectory
    • Relative browser window
    • Relative document
    • Relative browser window
    • Relative document
Convert JavaScript to get the coordinates of the mouse clicking position

In some Dom operations, we often deal with the location of elements, and a common aspect of mouse interaction, it is disappointing that different browsers may have different results or even some browsers have no results. In this article, we will make some simple summary by clicking the coordinates of the positions and clicking the positions, without any special statement.CodeTest compatibility in IE8, Firefox, and chrome

 

Mouse Click coordinates

Relative to the screen

If it is relatively simple to determine the position of a mouse click, after obtaining the mouse click event, the event screenx and screeny obtain the left margin and top margin of the click position relative to the screen, if the IFRAME factor is not taken into account, the performance in different browsers is still consistent.

 
FunctionGetmousepos (event ){VaRE = event |Window. event;Return{'X': E. screenx, 'y': Screeny }}

 

Relative browser window

Simple code can be implemented, but this is not enough, because in most cases we want to obtain the coordinates of the mouse position relative to the browser window, the clientx of event, the clienty attribute indicates the left margin and top margin of the cursor position relative to the document. So we wrote such code.

 
FunctionGetmousepos (event ){VaRE = event |Window. event;Return{'X': E. clientx, 'y': Clienty }}

 

Relative document

There is no problem with a simple test, but clientx and clienty obtain the coordinates relative to the current screen, ignoring the Page scrolling factor, which is useful in many conditions, but what should we do when we need to consider Page scrolling, that is, the coordinates relative to the document (Body element? Add the scroll displacement. Next we will try to calculate the page scroll displacement.

In fact, the problem in Firefox is much simpler, because Firefox supports pagex and Pagey attributes, which have included Page scrolling.

Extends umentelement. scrollleft, document.doc umentelement. scrolltop

FunctionGetmousepos (event ){VaRE = event |Window. event;VaRScrollx = document.doc umentelement. scrollleft |Document. Body. scrollleft;VaRScrolly = document.doc umentelement. scrolltop |Document. Body. scrolltop;VaRX = E. pagex | E. clientx +Scrollx;VaRY = E. Pagey | E. clienty +Scrolly;//Alert ('x: '+ x +' \ NY: '+ y );Return{'X': X, 'y': Y };}

In some Dom operations, we often deal with the location of elements, and a common aspect of mouse interaction, it is disappointing that different browsers have different results or even some browsers have no results. In this article, we will give a simple summary by clicking the coordinates of the positions, no special declaration code is used to test compatibility in IE8, Firefox, and chrome.

 

Mouse Click coordinates

Relative to the screen

If it is relatively simple to determine the position of a mouse click, after obtaining the mouse click event, the event screenx and screeny obtain the left margin and top margin of the click position relative to the screen, if the IFRAME factor is not taken into account, the performance in different browsers is still consistent.

 
FunctionGetmousepos (event ){VaRE = event |Window. event;Return{'X': E. screenx, 'y': Screeny }}

 

Relative browser window

Simple code can be implemented, but this is not enough, because in most cases we want to obtain the coordinates of the mouse position relative to the browser window, the clientx of event, the clienty attribute indicates the left margin and top margin of the cursor position relative to the document. So we wrote such code.

 
FunctionGetmousepos (event ){VaRE = event |Window. event;Return{'X': E. clientx, 'y': Clienty }}

 

Relative document

There is no problem with a simple test, but clientx and clienty obtain the coordinates relative to the current screen, ignoring the Page scrolling factor, which is useful in many conditions, but what should we do when we need to consider Page scrolling, that is, the coordinates relative to the document (Body element? Add the scroll displacement. Next we will try to calculate the page scroll displacement.

In fact, the problem in Firefox is much simpler, because Firefox supports pagex and Pagey attributes, which have included Page scrolling.

Extends umentelement. scrollleft, document.doc umentelement. scrolltop

 
FunctionGetmousepos (event ){VaRE = event |Window. event;VaRScrollx = document.doc umentelement. scrollleft |Document. Body. scrollleft;VaRScrolly = document.doc umentelement. scrolltop |Document. Body. scrolltop;VaRX = E. pagex | E. clientx +Scrollx;VaRY = E. Pagey | E. clienty +Scrolly;//Alert ('x: '+ x +' \ NY: '+ y );Return{'X': X, 'y': Y };}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.