Home > Web Front-end > JS Tutorial > body text

How to get the position of the scroll bar in javascript

王林
Release: 2021-11-01 15:29:52
Original
7650 people have browsed it

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template