Check whether HTML element has scrollbars

前端 未结 11 2170
北海茫月
北海茫月 2020-11-27 11:58

What\'s the fastest way of checking whether an element has scroll bars?

One thing of course is checking whether element is larger than its viewport, which can easily

11条回答
  •  时光取名叫无心
    2020-11-27 12:36

    For IE11 (Internet Explorer 11) I had to change the logic to:

    // Subtract 3 (a small arbitrary number) to allow for IE reporting a difference of 1 when no scrollbar is present
    var hasVerticalScrollbar = div.scrollHeight - 3 > div.clientHeight;
    

    This is because IE reports scrollHeight as 1 larger than clientHeight when no scrollbar is present but approx 9 larger when a scrollbar is present

提交回复
热议问题