Understand Why document. body. scrollTop is always 0?

Source: Internet
Author: User
<Style type = "text/css">
Body {width: 1500px; height: 1500px; padding-top: 200px}
</Style>
Add this style to make WebpageForm scrolling

1. Why?Document. Body. scrollTop is always 0?
Understanding:
First:
Not added at the top of the document <! DOCTYPEHtmlPUBLIC "-// W3C // DTDXHTML1.0 Transitional // "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<Html xmlns = "http://www.w3.org/1999/xhtml">, isBodyFor the document root element, because document.body.scrolltopic is valid, not 0, and document.doc umentElement. scrollTop is 0.

When you addDoctypeThe Declaration is based on the html element as the root element. Therefore, document. body. scrollTop is always 0,
Use document.documentelement.scrolltop(document.doc umentElement is the object of the html element)
The code can be written as follows:
VarScrolltop= Document. compatMode = "CSS1Compat "? Document.doc umentElement. scrollTop: document. body. scrollTop;
Understanding of document. compatMode
Obtain the current document rendering method. When the document has a standard declaration, the document. the value of compatMode is equal to "CSS1compat". Therefore, we can. compatMode value to determine whether a standard declaration is added to the document
Second point:
Documents must be rolled out
<Input type = "button" value = "body. scrollTop" onclick = "alert(document.doc umentElement. scrollTop)"/>

Summary:
1. The current webpage is basically declared <! DOCTYPE html generally uses document.doc umentElement. scrollTop and document.doc umentElement. scrollWidth.

Download the complete case: the deep-seated parameter is document.body.scrolltopic, Which is 0 million. rar (1.09 KB)

From: http://www.eyejs.com/html/29/n-29.html

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.