JavaScript obtains mouse absolute position program code

Source: Internet
Author: User

First, the analysis of event location properties in different browsers:

1. IE Event.x,event.y is the reference point (excluding the scrolling distance) from the outside of the parent element of the event triggering element.
2. Firefox's Event.pagex,event.pagey is based on the BODY element as the reference point (including scrolling distance)
3. Event.clientx,event.clienty to the top left corner of the browser (excluding scrolling distance)
4. IE's event.offsetx,event.offsety and Firefox's event.layerx,event.layery are the reference points in the upper-left corner of the event-triggering element (including scrolling distances, which may appear negative when there is a border).

And then the DOM object height attribute analysis

1. ScrollHeight: Gets the scrolling height of the object
2. ScrollLeft: Sets or gets the left distance between the left edge of the object and the current visible content in the window
3. ScrollTop: Sets or gets the distance between the top of the object and the top of the visible content in the window
4. ScrollWidth: Get the scrolling width of the object
5. Offsetheight: Gets the height of the object relative to the layout or the parent coordinates specified by the parent coordinate offsetparent property
6. Offsetleft: Gets the left position of the calculation of the object relative to the layout or the parent coordinates specified by the Offsetparent property
7. offsettop: Gets the calculated top position of the object relative to the layout or the parent coordinates specified by the offsettop property


With the above analysis, write out the two function of taking position

The code is as follows Copy Code

Take X axis position
function MouseX (evt) {
Firefox
if (Evt.pagex) return evt.pagex;
Ie
else if (EVT.CLIENTX)
return Evt.clientx + (Document.documentElement.scrollLeft document.documentElement.scrollLeft: Document.body.scrollLeft);
else return null;
}
Take the y-axis position
function Mousey (evt) {
Firefox
if (evt.pagey) return evt.pagey;
Ie
else if (evt.clienty)
return Evt.clienty + (Document.documentElement.scrollTop document.documentElement.scrollTop: DOCUMENT.BODY.SCROLLTOP);
else return null;
}

Two ways to get the absolute position of an HTML control

The code is as follows Copy Code

function Getabspoint (e) {
var x = e.offsetleft, y = e.offsettop;
while (E = e.offsetparent) {
x + + E.offsetleft;
Y + + e.offsettop;
}
Alert ("x:" + x + "," + "y:" + y);
}

function Getabspoint (obj) {
var x, y;
Orect = Obj.getboundingclientrect ();
x = Orect.left
y = orect.top
Alert ("+ x +", "+ y +"))
}

Attention

The

Document.body.scrollleft,document.body.scrolltop is used only for IE6 previous versions, in IE6, for declaring DOCTYPE, or declaring transitional DOCTYPE, Then IE6 will use Document.documentElement.scrollLeft to get the absolute position of the mouse

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.