How can I check if a scrollbar is visible?

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

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

For example:

HTML

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

    Find a parent of current element that has vertical scrolling or body.

    $.fn.scrollableParent = function() {
        var $parents = this.parents();
    
        var $scrollable = $parents.filter(function(idx) {
            return this.scrollHeight > this.offsetHeight && this.offsetWidth !== this.clientWidth;
        }).first();
    
        if ($scrollable.length === 0) {
            $scrollable = $('html, body');
        }
        return $scrollable;
    };
    

    It may be used to autoscroll to current element via:

    var $scrollable = $elem.scrollableParent();
    $scrollable.scrollTop($elem.position().top);
    
    0 讨论(0)
提交回复
热议问题