To locate the height of an HTML element (for example, div) at the top of the page, if you want to use offsetTop to set the value, you can find that if float: left is used in CSS, it may lead to different values.
Float: left is not used; the value obtained is normal 200;
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> influence of Float on offsetTop </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Meta name = "Copyright" content = "(c)"/>
<Meta name = "Author" content = "CNLei"/>
<Style type = "text/css" media = "all">
Body {margin: 0; padding: 0 ;}
# Top {height: 100px ;}
# Main {padding: 100px ;}
# IECN {width: 200px; height: 50px; background: # f00; color: # fff ;}
</Style>
<Script type = "text/javascript">
<! --
Function ShowIt (o ){
Alert (o. offsetTop );
}
-->
</Script>
</Head>
<Body>
<Div id = "Top"> # Main {padding: 100px;} the float attribute is not used. The obtained offsetTop value is 200. </Div>
<Div id = "Main">
<Div id = "IECN" onclick = "ShowIt (this);"> click here to try it </div>
</Div>
</Body>
</Html>
After float: left is used, the value is changed to 100, and 100 is missing for no reason.
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> influence of Float on offsetTop </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Meta name = "Copyright" content = "(c)"/>
<Meta name = "Author" content = "CNLei"/>
<Style type = "text/css" media = "all">
Body {margin: 0; padding: 0 ;}
# Top {height: 100px ;}
# Main {padding: 100px; float: left}
# IECN {width: 200px; height: 50px; background: # f00; color: # fff ;}
</Style>
<Script type = "text/javascript">
<! --
Function ShowIt (o ){
Alert (o. offsetTop );
}
-->
</Script>
</Head>
<Body>
<Div id = "Top"> # Main {padding: 100px; float: left;} uses the float attribute. The obtained offsetTop value is only 100, which is 100 less for no reason. </Div>
<Div id = "Main">
<Div id = "IECN" onclick = "ShowIt (this);"> click here to try it </div>
</Div>
</Body>
</Html>