Meaning: scroll bar height
Functions: scroll loading (ajax), scroll navigation fixed positioning, scrolling bullet frame positioning, etc.
Shows the effects of scrolling navigation and sidebar scrolling fixed positioning:
1. Chrome browser
Both document.body.scrollTop and document.documentElement.scrollTop are OK
2. The differences between scrollTop in each browser
IE6/7/8/9/10:
For pages without doctype declaration, you can use document.body.scrollTop to get the scrollTop height;
For pages with doctype declarations, you can use document.documentElement.scrollTop;
Safari:
safari is quite special, and it has its own function to obtain scrollTop: window.pageYOffset;
Firefox:
Firefox and other relatively standard browsers are much more worry-free, just use them directly
document.documentElement.scrollTop;
3. Get scrollTop value
Perfect to get scrollTop assignment phrase:
var scrollTop = document.documentElement.scrollTop||window.pageYOffset||document.body.scrollTop;
ps: Problem in scrolltop usage in javascript
document.documentElement.scrollTop refers to the vertical coordinates of the scroll bar
document.documentElement.clientHeight refers to the height of the visible area of the browser.
document.documentElement.clientHeight-oDiv is the initial vertical coordinate of the suspended box (relative to the top value of the body) (this value is fixed)
However, when you pull the scroll bar, the vertical coordinate (target) of the floating frame must be increased or decreased based on the initial coordinate to obtain the effect of visually scrolling with the scroll bar. The increase or decrease value is the distance the scroll bar pulls, that is, your scrollTop
Let me tell you what the difference between scrollTop and offsetTop in javascript
scrollTop refers to the distance at which a scrollable block scrolls downward, such as scrolling downward by 10 pixels, then the scrollTop attribute value of this element is 10;
offsetTop is the absolute distance between the upper border of the element and the upper border of the parent element.
The two do not describe the same thing, so there is no comparison.
The above is the full description of scrollTop in JavaScript introduced to you by the editor. I hope it will be helpful to you. If you want to know more, please pay attention to Wulin.com!