JS is perfectly compatible with the scrolltop methods of various browsers
1. Differences of scrollTop in different browsers
IE6/0/6/7/8/9/10:
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, the window under 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;
This is a perfect assignment statement to obtain scrollTop.