How can I check if a scrollbar is visible?

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

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

For example:

HTML

19条回答
  •  囚心锁ツ
    2020-11-22 15:31

    Most of the answers presented got me close to where I needed to be, but not quite there.

    We basically wanted to assess if the scroll bars -would- be visible in a normal situation, by that definition meaning that the size of the body element is larger than the view port. This was not a presented solution, which is why I am submitting it.

    Hopefully it helps someone!

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

    Essentially, we have the hasScrollbar function, but returning if the requested element is larger than the view port. For view port size, we just used $(window).height(). A quick compare of that against the element size, yields the correct results and desirable behavior.

提交回复
热议问题