I need to determine if there are scrollbars (both vertical and horizontal) in the browser window. I used this code, but it does not work reliably in Firefox 5.
JFL.GetScrollbarState = function () { var myWidth = 0; var myHeight = 0; if (document.documentElement && document.documentElement.clientWidth) { myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight; } else { myWidth = document.body.clientWidth; myHeight = document.body.clientHeight; } return ({ vScrollbar: document.body.scrollHeight > myHeight, hScrollbar: document.body.scrollWidth > myWidth }); }
Is there a better way to do this to work with cross browser. My browser goals are Firefox 4-5, Chrome, Safari 4+, Opera 10+.
If you're wondering why I need to know if there are scrollbars, this is because I have some CSS3 rotary transitions that (due to the nature of their rotation) may temporarily go beyond the current size of the document (thus making the document temporarily larger). If scrollbars were not initially present, the CSS3 transition can cause the scroll bars to appear during the transition, and then go away when the transition is finished, resulting in an ugly scroll flash. If I know that there are no scrollbars, I can temporarily add a class that will hide overflow-x or overflow-y and thus prevent the scrollbar from scrolling during the CSS3 transition. If the scrollbars are already present, I donβt need to do anything because they can move a little, but they will not turn on / off during the transition.
Bonus points if you can really tell not only if scrolling is usually required, but they are actually there or not.
source share