Home > Article > Web Front-end > How to get the position of the scroll bar in javascript
Javascript method to get the position of the scroll bar: [if($.browser.webkit){scTop=$("body").scrollTop()}else{scTop =document.documentElement.scrollTop].
#The operating environment of this article: windows10 system, javascript 1.8.5, thinkpad t480 computer.
In javascript, we usually have two ways to get the position of the scroll bar in the browser. Next, let us take a look at these two ways:
The first one: document. documentElement.scrollTop
This method is compatible with FireFox and IE browsers, but the value obtained for the Chrome browser is always 0.
Second type: $("body").scrollTop()
This method is compatible with chrome browsers, but the value in IE and FireFox browsers is always 0.
So the most accurate way to get the location is:
if($.browser.webkit) { scTop = $("body").scrollTop(); }else { scTop = document.documentElement.scrollTop; }
Recommended learning: javascript video tutorial
The above is the detailed content of How to get the position of the scroll bar in javascript. For more information, please follow other related articles on the PHP Chinese website!