使用 jQuery 检测 DIV 中是否存在滚动条?
我想使用 jQuery 检测 DIV 中是否存在滚动条。我正在考虑使用 $('div').scrollTop()
但当滚动条位于顶部和根本没有滚动条时,这两种情况都会返回 0。
大家有什么想法吗?
I want to detect the presence of a scroll bar in a DIV using jQuery. I was thinking to use $('div').scrollTop()
but that returns 0 in both cases when the scroll bar is at the top and when there is no scroll bar at all.
Any ideas guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设 div 上的
overflow
为auto
:Assuming
overflow
on the div isauto
:例子:
example:
好吧,我最终通过执行以下操作找到了解决方案:
包装随 DIV 增长的内容,然后通过将
wrapperDiv
的高度与containerDiv
(如果内容太大,通常会有滚动条)。如果wrapperDiv的高度大于containerDiv的高度则有滚动条,如果小于则没有滚动条。
Well I ended up finding a solution by doing the following:
Wrap the content that grows with a DIV, then I detect if a (vertical) scroll bar is present by comparing the height of
wrapperDiv
with the height ofcontainerDiv
(which normally has the scroll bar if the content is too large).If the height of
wrapperDiv
is bigger than the height ofcontainerDiv
then there is a scroll bar, if it is smaller, then there is no scroll bar.我将修改上面提到的 bobince,因为你要求 jQuery
这是因为
scrollHeight
和scrollWidth
是 DOM 属性。I will revise what bobince mentioned above since you are asking for jQuery
This is because
scrollHeight
andscrollWidth
are DOM properties.