How can I determine the height of a horizontal scrollbar, or the width of a vertical one, in JavaScript?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
From Alexandre Gomes Blog I have not tried it. Let me know if it works for you.
This is only script I've found, which is working in webkit browsers ... :)
Minimized version:
And you have to call it when document is ready ... so
Tested 2012-03-28 on Windows 7 in latest FF, Chrome, IE & Safari and 100% working.
source: http://benalman.com/projects/jquery-misc-plugins/#scrollbarwidth
Create an empty
div
and make sure it's present on all pages (i.e. by putting it in theheader
template).Give it this styling:
Then simply check for the size of
#scrollbar-helper
with Javascript:No need to calculate anything, as this div will always have the
width
andheight
of thescrollbar
.The only downside is that there will be an empty
div
in your templates.. But on the other hand, your Javascript files will be cleaner, as this only takes 1 or 2 lines of code.With jquery (only tested in firefox):
But I actually like @Steve's answer better.
From David Walsh's blog:
Gives me 17 on my website, 14 here on Stackoverflow.
This is a great answer: https://stackoverflow.com/a/986977/5914609
However in my case it did not work. And i spent hours searching for the solution.
Finally i've returned to above code and added !important to each style. And it worked.
I can not add comments below the original answer. So here is the fix: