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 };}