Use of javascript scrollTop

Source: Internet
Author: User

Javascript scrollTop obtains the offset of the scroll bar relative to its top (for example, creating an automatically hidden "Back to Top" button ). In practical applications, you may encounter the following problems:
Document.doc umentElement. scrollTop is 0 in Chrome.
Document. body. scrollTop is always 0 in IE and firefox

1. Differences between windows. pageYOffset/document.doc umentElement. scrollTop/document. body. scrollTop in different browsers
Example:
Copy codeThe Code is as follows:
Window. scroll (0,100)
Console. log ("window. pageYOffset:" + window. pageYOffset)
Console.log(registrdocument.doc umentElement. scrollTop: registry.document.doc umentElement. scrollTop)
Console. log ("document. body. scrollTop:" + document. body. scrollTop)

IE6/7/8:
Doctype:
Window. pageYOffset: undefined
Document.doc umentElement. scrollTop: 100
Document. body. scrollTop: 0

No doctype:
Window. pageYOffset: undefined
Document.doc umentElement. scrollTop: 0
Document. body. scrollTop: 100
Safari/Chrome:
Window. pageYOffset: 100
Document.doc umentElement. scrollTop: 0
Document. body. scrollTop: 100

Firefox/Opera:

Doctype:
Window. pageYOffset: 100
Document.doc umentElement. scrollTop: 100
Document. body. scrollTop: 0

No doctype:
Window. pageYOffset: 100
Document.doc umentElement. scrollTop: 0
Document. body. scrollTop: 100

2. Obtain the scrollTop Value
Perfect short for getting scrollTop assignments:
Copy codeThe Code is as follows:
Var scrollTop = window. pageYOffset | document.doc umentElement. scrollTop | document. body. scrollTop;

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.