Obtains the absolute bit value of the current element.

Source: Internet
Author: User
In the web design, you often need to obtain the absolute bit value of the element so that the pop-up layer is not deformed on the page. So we need to get the absolute coordinates of the clicked element. When you look at Alibaba's JS, you can see one that supports three browsers, so you can paste the following below:
The call method is simple:
<Input type = "button" onclick = "show (this)" id = "button2" value = "clickme"/> <br/> < Script Type = " Text/JavaScript " >
Function Show (checkel)
{
Document. getelementbyid ( " Tishidiv " ). Style. Display =   " Block " ;
Document. getelementbyid ( " Tishidiv " ). Style. Position =   " Absolute " ;
Document. getelementbyid ( " Tishidiv " ). Style. Top = Getxy (checkel )[ 1 ] + 12 + " Px " ;
Document. getelementbyid ( " Tishidiv " ). Style. Left = Getxy (checkel )[ 0 ] - 10 + " Px " ;

}
/* *
* Obtain the X and Y coordinates of the El object.
* @ Param {object} el
  */
Function Getxy (EL ){
VaR Pos;
If ( This . Getexplorertype () = 1 ){
VaR Scrolltop = Math.max(document.doc umentelement. scrolltop, document. Body. scrolltop );
VaR Scrollleft = Math.max(document.doc umentelement. scrollleft, document. Body. scrollleft );
Pos = [El. getboundingclientrect (). Left + Scrollleft, El. getboundingclientrect (). Top + Scrolltop];
} Else {
Pos = [El. offsetleft, El. offsettop];
VaR Parentnode = El. offsetparent;
If (Parentnode ! = El ){
While (Parentnode ){
Pos [ 0 ] + = Parentnode. offsetleft;
Pos [ 1 ] + = Parentnode. offsettop;
Parentnode = Parentnode. offsetparent;
}
}
If (EL. parentnode) {parentnode = El. parentnode ;}
Else {Parentnode =   Null ;}

While (Parentnode && Parentnode. tagname. touppercase () ! = 'Body' && Parentnode. tagname. touppercase () ! = 'Html '){
If (Parentnode. style. Display ! = 'Line '){
Pos [ 0 ] -= Parentnode. scrollleft;
Pos [ 1 ] -= Parentnode. scrolltop;
}

If (Parentnode. parentnode ){
Parentnode = Parentnode. parentnode;
} Else {Parentnode =   Null ;}
}
}
Return Pos;
}
Function Getassumertype (){
VaR UA = Navigator. useragent. tolowercase ();
If (Window. activexobject ){
Return   1 ;
} Else   If (UA. indexof ('Firefox ') >- 1 )){
Return   2 ;
} Else   If (UA. indexof ('Opera ') >- 1 )){
Return   3 ;
}
}
</ Script >

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.