乐闻世界logo
搜索文章和话题

How can i check if a scrollbar is visible

6个答案

1
2
3
4
5
6

Checking if a scrollbar is visible on a webpage typically involves several methods, commonly using JavaScript or CSS styles to determine. Here are several methods to check if a scrollbar is visible:

Using JavaScript

Method 1: Check for Vertical Scrollbar Visibility by Comparing Element Height

javascript
function isScrollbarVisible(element) { return element.scrollHeight > element.clientHeight; } // Usage let element = document.getElementById('myElement'); // The element to check let scrollbarVisible = isScrollbarVisible(element); console.log('Is scrollbar visible:', scrollbarVisible);

Method 2: Check for Horizontal Scrollbar Visibility by Comparing Element Width

javascript
function isHorizontalScrollbarVisible(element) { return element.scrollWidth > element.clientWidth; } function isVerticalScrollbarVisible(element) { return element.scrollHeight > element.clientHeight; } // Usage let element = document.getElementById('myElement'); // The element to check let horizontalScrollbarVisible = isHorizontalScrollbarVisible(element); let verticalScrollbarVisible = isVerticalScrollbarVisible(element); console.log('Is horizontal scrollbar visible:', horizontalScrollbarVisible); console.log('Is vertical scrollbar visible:', verticalScrollbarVisible);

Using CSS

With CSS, you can adjust the overflow property of an element to control scrollbar visibility. This is not a detection method for whether a scrollbar is visible; instead, it controls when scrollbars are displayed.

css
/* Always show vertical scrollbar */ .element-always-show-vertical-scrollbar { overflow-y: scroll; } /* Show vertical scrollbar when needed */ .element-auto-vertical-scrollbar { overflow-y: auto; } /* Hide vertical scrollbar */ .element-hide-vertical-scrollbar { overflow-y: hidden; } /* For horizontal scrollbars, replace 'y' with 'x' */

Note that these methods detect scrollbar visibility at the element level. If you need to check for scrollbar visibility on the entire page, replace element with document.body or document.documentElement.

2024年6月29日 12:07 回复

This extends @Reigel's answer. It returns whether the horizontal or vertical scrollbar is visible.

javascript
(function($) { $.fn.hasScrollBar = function() { var e = this.get(0); return { vertical: e.scrollHeight > e.clientHeight, horizontal: e.scrollWidth > e.clientWidth }; } })(jQuery);

For example:

  • element.hasScrollBar() returns an object with vertical and horizontal properties, each being true or false
  • element.hasScrollBar().vertical returns true or false
  • element.hasScrollBar().horizontal returns true or false
2024年6月29日 12:07 回复

I should modify what Reiger stated:

javascript
(function($) { $.fn.hasScrollBar = function() { return this[0] ? this[0].scrollHeight > this.innerHeight() : false; } })(jQuery);

innerHeight calculates the height of the control and its top and bottom padding.

2024年6月29日 12:07 回复

This might be a simpler solution. ```javascript if ($(document).height() > $(window).height()) { // scrollbar }

shell
2024年6月29日 12:07 回复

You can use the combination of the Element.scrollHeight and Element.clientHeight properties to accomplish this.

According to MDN:

The Element.scrollHeight read-only property measures the height of the element's content, including content that is not visible on the screen due to overflow. The scrollHeight value equals the minimum clientHeight required for the element to accommodate all content within the viewport without using a vertical scrollbar. It includes the element's padding but not its margins.

The Element.clientHeight read-only property returns the internal height of the element (in pixels), including padding but excluding the height of the horizontal scrollbar, borders, or margins.

clientHeight can be calculated as CSS height plus CSS padding minus the height of the horizontal scrollbar (if present).

Therefore, if the scrollHeight is greater than the clientHeight, the element will display a scrollbar. Hence, the answer to your question is:

javascript
function scrollbarVisible(element) { return element.scrollHeight > element.clientHeight; }
2024年6月29日 12:07 回复

A small plugin.

javascript
(function($) { $.fn.hasScrollBar = function() { return this.get(0).scrollHeight > this.height(); } })(jQuery);

Use it like this: $('#my_div1').hasScrollBar(); // returns true if a vertical scrollbar is present, false otherwise.

Tested to work on Firefox, Chrome, IE6, 7, and 8. The body selector does not work properly with tag selectors.

Demo

      • Edit I found that this function doesn't work when a horizontal scrollbar causes a vertical scrollbar to appear... I found another solution... using clientHeight return this.get(0).scrollHeight > this.get(0).clientHeight;
2024年6月29日 12:07 回复

你的答案