How can I check if a scrollbar is visible?

后端 未结 19 2603
情话喂你
情话喂你 2020-11-22 14:39

Is it possible to check the overflow:auto of a div?

For example:

HTML

相关标签:
19条回答
  • 2020-11-22 15:20

    Here's my improvement: added parseInt. for some weird reason it didn't work without it.

    // usage: jQuery('#my_div1').hasVerticalScrollBar();
    // Credit: http://stackoverflow.com/questions/4814398/how-can-i-check-if-a-scrollbar-is-visible
    (function($) {
        $.fn.hasVerticalScrollBar = function() {
            return this.get(0) ? parseInt( this.get(0).scrollHeight ) > parseInt( this.innerHeight() ) : false;
        };
    })(jQuery);
    
    0 讨论(0)
  • 2020-11-22 15:24

    I should change a little thing of what Reigel said:

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

    innerHeight counts control's height and its top and bottom paddings

    0 讨论(0)
  • 2020-11-22 15:24

    This expands on @Reigel's answer. It will return an answer for horizontal or vertical scrollbars.

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

    Example:

    element.hasScrollBar()             // Returns { vertical: true/false, horizontal: true/false }
    element.hasScrollBar().vertical    // Returns true/false
    element.hasScrollBar().horizontal  // Returns true/false
    
    0 讨论(0)
  • 2020-11-22 15:25

    I'm going to extend on this even further for those poor souls who, like me, use one of the modern js frameworks and not JQuery and have been wholly abandoned by the people of this thread :

    this was written in Angular 6 but if you write React 16, Vue 2, Polymer, Ionic, React-Native, you'll know what to do to adapt it. And it's the whole component so it should be easy.

    import {ElementRef, AfterViewInit} from '@angular/core';
    
    @Component({
      selector: 'app',
      templateUrl: './app.html',
      styleUrls: ['./app.scss']
    })
    export class App implements AfterViewInit {
    scrollAmount;
    
    constructor(
      private fb: FormBuilder,
      private element: ElementRef 
    ) {}
    
    ngAfterViewInit(){
      this.scrollAmount = this.element.nativeElement.querySelector('.elem-list');
      this.scrollAmount.addEventListener('wheel', e => { //you can put () instead of e
      // but e is usefull if you require the deltaY amount.
        if(this.scrollAmount.scrollHeight > this.scrollAmount.offsetHeight){
           // there is a scroll bar, do something!
        }else{
           // there is NO scroll bar, do something!
        }
      });
    }
    }
    

    in the html there would be a div with class "elem-list" which is stylized in the css or scss to have a height and an overflow value that isn't hidden. (so auto or sroll )

    I trigger this eval upon a scroll event because my end goal was to have "automatic focus scrolls" which decide whether they are scrolling the whole set of components horizontally if said components have no vertical scroll available and otherwise only scroll the innards of one of the components vertically.

    but you can place the eval elsewhere to have it be triggered by something else.

    the important thing to remember here, is you're never Forced back into using JQuery, there's always a way to access the same functionalities it has without using it.

    0 讨论(0)
  • 2020-11-22 15:25

    There's two area sizes to consider, the window and the html. If the html width, for example, is greater than window width then a scroll bar may be present on the user interface. So it's a matter of reading the window proportions and the html proportions and doing the basic math.

    As for displaying an arrow overlaid atop the page, that's done with a simple classlist toggle, e.g. .hidden{display:none}

    Here's a crossbrowser method for getting these proportions. (credit W3 Schools)

    || document.body.clientWidth;
    
    var h = window.innerHeight || document.documentElement.clientHeight ||
    document.body.clientHeight; ```
    
    0 讨论(0)
  • 2020-11-22 15:27

    Here's an improved version of Evan's answer which seems to properly account for overflow logic.

                function element_scrollbars(node) {
                    var element = $(node);
                    var overflow_x = element.css("overflow-x");
                    var overflow_y = element.css("overflow-y");
                    var overflow = element.css("overflow");
                    if (overflow_x == "undefined") overflow_x == "";
                    if (overflow_y == "undefined") overflow_y == "";
                    if (overflow == "undefined") overflow == "";
                    if (overflow_x == "") overflow_x = overflow;
                    if (overflow_y == "") overflow_y = overflow;
                    var scrollbar_vertical = (
                        (overflow_y == "scroll")
                        || (
                            (
                                (overflow_y == "hidden")
                                || (overflow_y == "visible")
                            )
                            && (
                                (node.scrollHeight > node.clientHeight)
                            )
                        )
                    );
                    var scrollbar_horizontal = (
                        (overflow_x == "scroll")
                        || (
                            (
                                (overflow_x == "hidden")
                                || (overflow_x == "visible")
                            )
                            && (
                                (node.scrollWidth > node.clientWidth)
                            )
                        )
                    );
                    return {
                        vertical: scrollbar_vertical,
                        horizontal: scrollbar_horizontal
                    };
                }
    
    0 讨论(0)
提交回复
热议问题