1, the different browsers under the scrolltop difference
IE6/7/8/9/10:
Document.body.scrollTop can be used to get the scrolltop height for pages without DOCTYPE declarations;
For pages with DOCTYPE declarations, you can use Document.documentElement.scrollTop;
Safari:
Safari is special, has its own to obtain scrolltop function: Window.pageyoffset;
Firefox:
Firefox and so on relatively standard browser is much more worry, directly with Document.documentElement.scrollTop;
2. Get scrolltop Value
Perfect for getting scrolltop assignment phrases:
Copy Code code as follows:
var scrolltop = Document.documentElement.scrollTop | | Window.pageyoffset | | Document.body.scrollTop;
The value of scrolltop can be obtained in any case by the assignment of this sentence.
Look at the assignment, what do you see?
Yes, Window.pageyoffset (Safari) is placed in | | The middle position.
Because the system returns the last value by default when the number 0 is in the undefine or operation. In the operation of 0 = = Undefine;
When the page scroll bar is just at the top, that is, the scrolltop value is 0 o'clock, IE window.pageyoffset (Safari) returns to Undefine, when Window.pageyoffset (Safari) is placed on or at the end of the operation, ScrollTop return undefine, undefine use in the next operation will be an error.
Other browsers do not return undefine regardless of scrolltop assignment or order of operations. Safe to use.
So the end is still ie problem. Cup with ...
Spirit a little trance, do not know whether there is no clear expression.
But finally summed up this sentence experiment is OK, we rest assured use.
Copy Code code as follows:
var scrolltop = Document.documentElement.scrollTop | | Window.pageyoffset | | Document.body.scrollTop;
It's perfect. Gets the ScrollTop assignment statement.
The above mentioned is the entire content of this article, I hope you can enjoy.