Home  >  Article  >  Web Front-end  >  How to get the position of the scroll bar in javascript

How to get the position of the scroll bar in javascript

王林
王林Original
2021-11-01 15:29:527636browse

Javascript method to get the position of the scroll bar: [if($.browser.webkit){scTop=$("body").scrollTop()}else{scTop =document.documentElement.scrollTop].

How to get the position of the scroll bar in javascript

#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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more