I am trying to create my own scroll bars. I have tried most of the jquery scrollbar plugins and none of them seem to work for me, so I decided to create my own. I have an overflow area with scrollable content. I can get the scrollbar to work if I am able to figure out the height of the scrollable content area. I have tried .scrollHeight() and the browser doesn't even recognize it. The overflow area has a fixed position.
相关问题
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Include empty value fields in jQuery .serialize()
- Disable Browser onUnload on certain links?
- how to get selected text from iframe with javascri
You can get it with
.outerHeight()
.Sometimes, it will return
0
. For the best results, you can call it in yourdiv
's ready event.To be safe, you should not set the height of the
div
tox
. You can keep its heightauto
to get content populated properly with the correct height.You can find a detailed post here.
scrollHeight
is not only buggy, but doesn't work when your container has a hardcoded height (which is probably most cases, since you wanna get contents height without doingcontainer.height()
itself)Of course, this latter alternative only works when
#outer
doesn't have immediate text childrens that take up space and you want to measure. Those are my 2 cents.<div>
of which you can measure easily by doing$('#outer').children().height()
scrollHeight
is a property of a DOM object, not a function:Given this:
This yields 200:
For example: http://jsfiddle.net/ambiguous/u69kQ/2/ (run with the JavaScript console open).
If you can possibly help it, DO NOT USE .scrollHeight.
.scrollHeight does not yield the same kind of results in different browsers in certain circumstances (most prominently while scrolling).
For example:
If you do
you'll get 900px in Chrome, FireFox and Opera. That's great.
But, if you attach a wheelevent / wheel event to #outer, when you scroll it, Chrome will give you a constant value of 900px (correct) but FireFox and Opera will change their values as you scroll down (incorrect).
A very simple way to do this is like so (a bit of a cheat, really). (This example works while dynamically adding content to #outer as well):
The reason I pick these three browsers is because all three can disagree on the value of .scrollHeight in certain circumstances. I ran into this issue making my own scrollpanes. Hope this helps someone.
We can also use -
Element.scrollHeight
is a property, not a function, as noted here. As noted here, the scrollHeight property is only supported after IE8. If you need it to work before that, temporarily set the CSSoverflow
andheight
toauto
, which will cause the div to take the maximum height it needs. Then get the height, and change the properties back to what they were before.