Solution to the document. body. scrollTop value is always 0

Source: Internet
Author: User
Use document.doc umentElement to specify doctype. During page creation, a fixed-position layer may be used to read document. body. scrollTop to set the layer position, as shown in the following figure:

Window. onscroll = function (){
Var oFix = document. getElementById ("pfix ");
OFix. style. top = document. body. scrollTop + "px ";
}

But why didn't it achieve the expected results? the output value of document. body. scrollTop is always 0. It turns out to be a problem with DTD. If the page starts with it, there will be no problem. However, to comply with the web standards, DTD is of course indispensable. Use document.doc umentElement. scrollTop to replace document. body. scrollTop with a DTD. ­

Window. onscroll = function (){
Var oFix = document. getElementById ("pfix ");
OFix. style. top = document.doc umentElement. scrollTop + "px ";
}

Editor's note:

When a page has a DTD (or DOCTYPE specified), use document.doc umentElement.
Document. body is used when a page does not have a DTD (or DOCTYPE is not specified.
This is true in both IE and Firefox.
For compatibility, you can use the following code: var scrollTop = window. pageYOffset
| Document.doc umentElement. scrollTop
| Document. body. scrollTop
| 0;
Related Article

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.