This time I will show you what properties of JS can be used to determine whether there is a scroll bar on the page, and what are the precautions for JS to determine whether there is a scroll bar on the page. Here is a practical case, let's take a look.
Preface
Recently in the process of writing a plug-in, I need to use JS to determine whether there is a scroll bar. After searching, the general methods are similar, but they are all a bit verbose. The code is not concise enough. Finally, by referring to different methods, I wrote a relatively simple method. When judging the scroll bar, you also need to calculate the width of the scroll bar. I will share it with you through this article.
Why should we judge the scroll bar
The need to judge the scroll bar is often used in pop-up window plug-ins, because most pop-up windows will add <a href="//m.sbmmt.com/wiki/923.html" target="_blank">overflow</a>: hidden
attribute. If the page is relatively long, the page will shake after adding this attribute.
In order to enhance the user experience, add the <a href="//m.sbmmt.com/wiki/937.html" target="_blank">margin-left</a>
attribute to offset the scroll bar after overflow: hidden
by determining whether there is a scroll bar. Location.
How to determine whether there is a scroll bar
In fact, only one line of JS is needed to test compatibility with IE7
function hasScrollbar() { return document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight); }
Generally, use document.body.scrollHeight
> window.innerHeight
can be judged.
But in IE7 and IE8, window.innerHeight
is underfined
, so in order to be compatible with IE7 and IE8, you need to use document.documentElement.clientHeight
Property calculates window height.
The method of calculating the scroll bar width
still takes the pop-up window as an example, because the scroll bars of IE 10 and above and mobile browsers do not occupy the page width. Transparent style (IE 10 and above browsers can restore the original scroll bar style through CSS properties), so in order to further enhance the user experience, we also need to calculate the width of the scroll bar and add a reasonable margin-left ## according to the situation. #Value.
offsetWidth and
clientWidth of the element. The difference of can be obtained. I will learn from the method in
Magnific-popup
function getScrollbarWidth() { var scrollp = document.createElement("p"); scrollp.style.cssText = 'width: 99px; height: 99px; overflow: scroll; position: absolute; top: -9999px;'; document.body.appendChild(scrollp); var scrollbarWidth = scrollp.offsetWidth - scrollp.clientWidth; document.body.removeChild(scrollp); return scrollbarWidth; }
Summary
Use JS to implement a The function may not be difficult, but as programmers, you should always think about how to implement this function more simply and elegantly, and always focus on improving the user experience. Forconditional judgment, maybe ten lines of logical judgment may only require one line. I have been deeply impressed recently, and I must be good at using ternary expressions instead of if..else to simplify the code.
visual areaheight.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:Detailed explanation of nginx deployment steps for vue.js project
Six commonly used JS sorting algorithms and comparison
Instructions for using the loader mechanism in webpack source code
The above is the detailed content of What properties does JS have to determine whether there is a scroll bar on the page?. For more information, please follow other related articles on the PHP Chinese website!