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 >