This article mainly introduces the scrollTop method in javascript and detailed testing of the compatibility of scrollTop in various browsers. it is very detailed and comprehensive. we recommend it to you here, if you have any need, refer. ScrollTop indicates the scroll height. by default, it is rolled down from position: 0. the offset of scrollTop (offset) indicates the offset relative to the top, in pixels,
The height of scrollTop () can be either 'set' or 'get.
When you set the scroll value, this method sets the scroll value of all matching elements.
When obtaining the scroll value, this method returns only the scroll position of the first matching element.
To obtain the scrollTop value, refer to the following code:
The code is as follows:
Var scrollTop = document.doc umentElement. scrollTop | window. pageYOfset | document. body. scrollTop;
1. differences of scrollTop in different browsers
IE6/7/8:
For pages without doctype declaration, you can use document. body. scrollTop to obtain the scrollTop height;
For pages with doctype declaration, you can use document.doc umentElement. scrollTop;
Safari:
Safari is quite special. you have a function to get scrollTop: window. pageYOffset;
Firefox:
Firefox and other relatively standard browsers are much more worry-free. you can directly use document.doc umentElement. scrollTop;
2. obtain the scrollTop value
ScrollTop assignment phrase:
The code is as follows:
Var scrollTop = document.doc umentElement. scrollTop | window. pageYOffset | document. body. scrollTop;
The scrollTop value can be obtained under any circumstances through this assignment.
After carefully observing the assignment, what did you find ??
That is, the window. pageYOffset (Safari) is placed in the center of |.
This is because the system returns the last value by default when the numbers 0 and undefine are used for or operations. 0 = undefine;
When the page scroll bar is at the top, that is, the scrollTop value is 0. Window in IE. pageYOffset (Safari) returns undefine. when pageYOffset (Safari) is placed at the end of the OR operation, scrollTop returns undefine. when undefine is used for subsequent operations, an error is reported.
Other browsers do not return undefine regardless of the value assignment or operation sequence of scrollTop. it can be used safely ..
So it's still a problem with IE. Cup...
I am a little confused and do not know whether to express myself clearly.
However, the experiment is too good, so you can use it with confidence;
The code is as follows:
Var scrollTop = document.doc umentElement. scrollTop | window. pageYOffset | document. body. scrollTop;
DTD description:
When a page has a DTD, or DOCTYPE is specified, document.doc umentElement is used.
Document. body is used when the page does not have a DTD or DOCTYPE is not specified.
This is true in both IE and Firefox.
To be compatible, use the following code regardless of whether there is a DTD:
The code is as follows:
Var scrollTop = window. pageYOffset // used for FF
| Document.doc umentElement. scrollTop
| Document. body. scrollTop
| 0;
DocumentElement and body:
Body is the body subnode in the DOM object, that isTag;
DocumentElement is the root node root of the entire node tree, that isTag;
DOM calls every object in a hierarchy a node, which is a hierarchy. you can think of it as a tree structure. just like our directory, a root directory contains subdirectories, there are also subdirectories under the subdirectory.
Take HTML hypertext markup language as an example: the root node of the entire document can be accessed using document.doc umentElement in dom. it is the root node of the entire node tree. The body is a subnode. to access the body tag, write "document. body" in the script.
If you want to click the button to scroll to the top of the page, use jquery to click and execute code $ (document). scrollTop (0) to scroll to the top.
The same scroll position scrollLeft indicates the scroll position to the left.
The above is all the content of this article. I hope you will like it.