Home Web Front-end JS Tutorial How to determine whether the page has scroll bars through JS

How to determine whether the page has scroll bars through JS

May 26, 2018 pm 03:36 PM

This time I will show you how to determine whether a page has scroll bars through JS, and what are the precautions for how to determine whether a page has scroll bars through JS. The following is a practical case, let's take a look.

Preface

Recently in the process of writing a plug-in, I need to use JS to determine whether there is a scroll bar. After searching, the general methods are similar, but they are all a bit verbose. The code is not concise enough. Finally, by referring to different methods, I wrote a relatively simple method. When judging the scroll bar, you also need to calculate the width of the scroll bar. I will share it with you through this article.

Why should we judge the scroll bar

The need to judge the scroll bar is often used in pop-up window plug-ins, because most pop-up windows will add Overflow: hidden attribute. If the page is relatively long, the page will shake after adding this attribute.

In order to enhance the user experience, add the margin-left attribute to offset the scroll bar position after overflow: hidden by determining whether there is a scroll bar.

How to determine whether there is a scroll bar

In fact, only one line of JS is needed to test compatibility with IE7

1

2

3

function hasScrollbar() {

  return document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight);

}

Copy after login

Generally, use document.body.scrollHeight > window.innerHeight can be judged.

But in IE7 and IE8, window.innerHeight is underfined, so in order to be compatible with IE7 and IE8, you need to use document.documentElement.clientHeight Property calculates window height.

The method of calculating the scroll bar width

still takes the pop-up window as an example, because the scroll bars of IE 10 and above and mobile browsers do not occupy the page width Transparent style (IE 10 and above browsers can restore the original scroll bar style through CSS properties), so in order to further enhance the user experience, we also need to calculate the width of the scroll bar and add a reasonable margin-left ## according to the situation. #Value.

The method to calculate the width of the scroll bar is relatively simple. Create a new p element with a scroll bar and pass the

offsetWidth and clientWidth of the element. The difference of can be obtained. I will learn from the method in Magnific-popup

1

2

3

4

5

6

7

8

function getScrollbarWidth() {

  var scrollp = document.createElement("p");

  scrollp.style.cssText = 'width: 99px; height: 99px; overflow: scroll; position: absolute; top: -9999px;';

  document.body.appendChild(scrollp);

  var scrollbarWidth = scrollp.offsetWidth - scrollp.clientWidth;

  document.body.removeChild(scrollp);

  return scrollbarWidth;

}

Copy after login

Summary

Use JS to implement a The function may not be difficult, but as programmers, you should always think about how to implement this function more simply and elegantly, and always focus on improving the user experience. For conditional judgment, perhaps ten lines of logical judgment may only require one line. I have been deeply impressed recently, and I must be good at using ternary expressions instead of

if..else to streamline the code.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use the webpack source code loader mechanism

How to use js css to achieve typing effect

The above is the detailed content of How to determine whether the page has scroll bars through JS. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

10 jQuery Syntax Highlighters 10 jQuery Syntax Highlighters Mar 02, 2025 am 12:32 AM

10 jQuery Syntax Highlighters

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

10  JavaScript & jQuery MVC Tutorials 10 JavaScript & jQuery MVC Tutorials Mar 02, 2025 am 01:16 AM

10 JavaScript & jQuery MVC Tutorials

See all articles