JavaScript to get the absolute position of the mouse

Source: Internet
Author: User




new document




<Body onmousemove = "getxy (event)">
<Script language = "JavaScript">
<! --
Function mousex (EVT ){
If (EVT. pagex) return EVT. pagex;
Else if (EVT. clientx)
Return EVT. clientx + (document.doc umentelement. scrollleft?
Document.doc umentelement. scrollleft:
Document. Body. scrollleft );
Else return NULL;
}
Function Mousey (EVT ){
If (EVT. Pagey) return EVT. Pagey;
Else if (EVT. clienty)
Return EVT. clienty + (document.doc umentelement. scrolltop?
Document.doc umentelement. scrolltop:
Document. Body. scrolltop );
Else return NULL;
}

Function getxy (Event)
{
VaR E = event | window. event;
VaR x = mousex (E );
Var y = Mousey (E );
Document. getelementbyid ("XY"). innerhtml = "X:" + x + "<br/> Y:" + Y;
}

Function getx (elementid)
{
VaR El = elementid
Return El. offsetleft

}

Function Gety (elementid)
{
VaR El = elementid
Return El. offsettop

}

Function getelementpositionx (elemid)
{
VaR offsettrail = Document. getelementbyid (elemid );
VaR offsetleft = 0;

While (offsettrail)
{
Offsetleft + = offsettrail. offsetleft;
Offsettrail = offsettrail. offsetparent;
}

If (navigator. useragent. indexof ("Mac ")! =-1 &&
Typeof (document. Body. leftmargin )! = "Undefined "){
Offsetleft + = Document. Body. leftmargin;
}

Return offsetleft;
}

Function getelementpositiony (elemid)
{
VaR offsettrail = Document. getelementbyid (elemid );
VaR offsettop = 0;

While (offsettrail)
{
Offsettop + = offsettrail. offsettop;
Offsettrail = offsettrail. offsetparent;
}

If (navigator. useragent. indexof ("Mac ")! =-1 &&
Typeof (document. Body. leftmargin )! = "Undefined "){
Offsettop + = Document. Body. topmargin;
}
Return offsettop;
}
Function getelementpositionxy (elemid)
{
VaR offsettrail = elemid;
VaR offsetleft = 24;
VaR offsettop = 0;

While (offsettrail)
{
Offsetleft + = offsettrail. offsetleft;
Offsettop + = offsettrail. offsettop;
Offsettrail = offsettrail. offsetparent;
}

If (navigator. useragent. indexof ("Mac ")! =-1 &
typeof (document. Body. leftmargin )! = "Undefined") {
offsetleft + = document. body. leftmargin;
offsetleft + = document. body. leftmargin;
}< br> document. getelementbyid ("txt_left "). innertext = offsetleft;
document. getelementbyid ("txt_top "). innertext = offsettop;
document. getelementbyid ("divmsg "). style. top = offsettop + "PX";
}

// -->

absolute cursor position:



absolute position of txtpos in the control input box:

X:

Y:




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.