如何获取溢出:隐藏或溢出:滚动div的真实.height()?
我有一个关于如何获得 div 高度的问题。我知道 .height()
和 innerHeight()
,但在这种情况下,它们都不能为我完成这项工作。问题是,在这种情况下,我有一个溢出宽度的 div:溢出:滚动,并且 div 具有固定的高度。
如果我使用 .height()
或 innerHeight()
,它们都会给我可见区域的高度,但如果我想考虑溢出,如何我去吗?
I have a question regarding how to get a div height. I'm aware of .height()
and innerHeight()
, but none of them does the job for me in this case. The thing is that in this case I have a div that is overflown width a overflow: scroll and the div has a fixed height.
If I use .height()
or innerHeight()
, both of them gives me the height of the visible area, but if I want the overflown taken in to account, how do I go about?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
.scrollHeight
DOM 节点的属性:$('#your_div')[0].scrollHeight
Use the
.scrollHeight
property of the DOM node:$('#your_div')[0].scrollHeight
有关
.scrollHeight
属性的更多信息,请参阅For more information about
.scrollHeight
property refer to the docs:另一种可能性是将 html 放置在屏幕“外部”的非溢出:隐藏元素中,例如绝对顶部和左侧小于 5000px 的位置,然后读取元素的高度。它很丑,但效果很好。
Another possibility would be to place the html in a non overflow:hidden element placed 'out' of screen, like a position absolute top and left less than 5000px, then read the element's height. It's ugly, but works well.
我刚刚为此制定了另一个解决方案,不再需要使用“非常高”的最大高度值。它需要几行 javascript 代码来计算折叠的 DIV 的内部高度,但之后就都是 CSS 了。
1) 获取和设置高度
获取折叠元素的内部高度(使用
scrollHeight
)。我的元素有一个类.section__accordeon__content
,我实际上在forEach()
循环中运行它来设置所有面板的高度,但你明白了。2) 使用 CSS 变量展开活动项目
接下来,当项目具有
.activemax-height
值代码>类。最终示例
因此,完整的示例如下所示:首先循环遍历所有折叠面板并将其
scrollHeight
值存储为 CSS 变量。接下来使用 CSS 变量作为元素的活动/展开/打开状态的max-height
值。Javascript:
CSS:
就这样。仅使用 CSS 和几行 JavaScript 代码(不需要 jQuery)的自适应最大高度动画。
希望这对将来的人有帮助(或我未来的自己供参考)。
I have just cooked up another solution for this, where it's not longer necessary to use a -much to high- max-height value. It needs a few lines of javascript code to calculate the inner height of the collapsed DIV but after that, it's all CSS.
1) Fetching and setting height
Fetch the inner height of the collapsed element (using
scrollHeight
). My element has a class.section__accordeon__content
and I actually run this in aforEach()
loop to set the height for all panels, but you get the idea.2) Use the CSS variable to expand the active item
Next, use the CSS variable to set the
max-height
value when the item has an.active
class.Final example
So the full example goes like this: first loop through all accordeon panels and store their
scrollHeight
values as CSS variables. Next use the CSS variable as themax-height
value on the active/expanded/open state of the element.Javascript:
CSS:
And there you have it. A adaptive max-height animation using only CSS and a few lines of JavaScript code (no jQuery required).
Hope this helps someone in the future (or my future self for reference).
另一个简单的解决方案(不是很优雅,但也不太难看)是放置一个内部
div / span
然后获取他的高度 ($(this).find('span).height( )
)。以下是使用此策略的示例:
(这个具体示例使用此技巧来设置 max-height 的动画并避免折叠时的动画延迟(当对 max-height 属性使用较大的数字时)。
Another simple solution (not very elegant, but not too ugly also) is to place a inner
div / span
then get his height ($(this).find('span).height()
).Here is an example of using this strategy:
(This specific example is using this trick to animate the max-height and avoiding animation delay when collapsing (when using high number for the max-height property).