How can I check if a scrollbar is visible?

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

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

For example:

HTML

19条回答
  •  被撕碎了的回忆
    2020-11-22 15:32

    a little plugin for it.

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

    use it like this,

    $('#my_div1').hasScrollBar(); // returns true if there's a `vertical` scrollbar, false otherwise..
    

    tested working on Firefox, Chrome, IE6,7,8

    but not working properly on body tag selector

    demo


    Edit

    I found out that when you have horizontal scrollbar that causes vertical scrollbar to appear, this function does not work....

    I found out another solution... use clientHeight

    return this.get(0).scrollHeight > this.get(0).clientHeight;
    

提交回复
热议问题