如何获取溢出:隐藏或溢出:滚动div的真实.height()?

发布于 2024-08-27 04:50:45 字数 249 浏览 4 评论 0原文

我有一个关于如何获得 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苦妄 2024-09-03 04:50:45

使用 .scrollHeight DOM 节点的属性:$('#your_div')[0].scrollHeight

Use the .scrollHeight property of the DOM node: $('#your_div')[0].scrollHeight

迷迭香的记忆 2024-09-03 04:50:45

有关 .scrollHeight 属性的更多信息,请参阅

Element.scrollHeight只读属性是元素内容高度的度量,包括由于溢出而在屏幕上不可见的内容。 scrollHeight 值等于元素所需的最小 clientHeight,以便在不使用垂直滚动条的情况下适应视点中的所有内容。它包括元素内边距,但不包括其边距。

For more information about .scrollHeight property refer to the docs:

The Element.scrollHeight read-only attribute is a measurement of the height of an element's content, including content not visible on the screen due to overflow. The scrollHeight value is equal to the minimum clientHeight the element would require in order to fit all the content in the viewpoint without using a vertical scrollbar. It includes the element padding but not its margin.

淡淡绿茶香 2024-09-03 04:50:45

另一种可能性是将 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.

孤独岁月 2024-09-03 04:50:45

我刚刚为此制定了另一个解决方案,不再需要使用“非常高”的最大高度值。它需要几行 javascript 代码来计算折叠的 DIV 的内部高度,但之后就都是 CSS 了。

1) 获取和设置高度

获取折叠元素的内部高度(使用scrollHeight)。我的元素有一个类 .section__accordeon__content ,我实际上在 forEach() 循环中运行它来设置所有面板的高度,但你明白了。

document.querySelectorAll( '.section__accordeon__content' ).style.cssText = "--accordeon-height: " + accordeonPanel.scrollHeight + "px";

2) 使用 CSS 变量展开活动项目

接下来,当项目具有 .activemax-height 值代码>类。

.section__accordeon__content.active {
  max-height: var(--accordeon-height);
}

最终示例

因此,完整的示例如下所示:首先循环遍历所有折叠面板并将其 scrollHeight 值存储为 CSS 变量。接下来使用 CSS 变量作为元素的活动/展开/打开状态的 max-height 值。

Javascript:

document.querySelectorAll( '.section__accordeon__content' ).forEach(
  function( accordeonPanel ) {
    accordeonPanel.style.cssText = "--accordeon-height: " + accordeonPanel.scrollHeight + "px";
  }
);

CSS:

.section__accordeon__content {
  max-height: 0px;
  overflow: hidden;
  transition: all 425ms cubic-bezier(0.465, 0.183, 0.153, 0.946);
}

.section__accordeon__content.active {
  max-height: var(--accordeon-height);
}

就这样。仅使用 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 a forEach() loop to set the height for all panels, but you get the idea.

document.querySelectorAll( '.section__accordeon__content' ).style.cssText = "--accordeon-height: " + accordeonPanel.scrollHeight + "px";

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.

.section__accordeon__content.active {
  max-height: var(--accordeon-height);
}

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 the max-height value on the active/expanded/open state of the element.

Javascript:

document.querySelectorAll( '.section__accordeon__content' ).forEach(
  function( accordeonPanel ) {
    accordeonPanel.style.cssText = "--accordeon-height: " + accordeonPanel.scrollHeight + "px";
  }
);

CSS:

.section__accordeon__content {
  max-height: 0px;
  overflow: hidden;
  transition: all 425ms cubic-bezier(0.465, 0.183, 0.153, 0.946);
}

.section__accordeon__content.active {
  max-height: var(--accordeon-height);
}

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).

孤独岁月 2024-09-03 04:50:45

另一个简单的解决方案(不是很优雅,但也不太难看)是放置一个内部 div / span 然后获取他的高度 ($(this).find('span).height( ))。

以下是使用此策略的示例:

$(".more").click(function(){
if($(this).parent().find('.showMore').length) {
$(this).parent().find('.showMore').removeClass('showMore').css('max-height','90px');
$(this).parent().find('.more').removeClass('less').text('More');
} else {
$(this).parent().find('.text').addClass('showMore').css('max-height',$(this).parent().find('span').height());
$(this).parent().find('.more').addClass('less').text('Less');
}
});
* {transition: all 0.5s;}
.text {position:relative;width:400px;max-height:90px;overflow:hidden;}
.showMore {}
.text::after {
  content: "";
    position: absolute; bottom: 0; left: 0;
        box-shadow: inset 0 -26px 22px -17px #fff;
    height: 39px;
  z-index:99999;
  width:100%;
  opacity:1;
}
.showMore::after {opacity:0;}
.more {border-top:1px solid gray;width:400px;color:blue;cursor:pointer;}
.more.less {border-color:#fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<div class="text">
<span>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</span></div>
<div class="more">More</div>
</div>

(这个具体示例使用此技巧来设置 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:

$(".more").click(function(){
if($(this).parent().find('.showMore').length) {
$(this).parent().find('.showMore').removeClass('showMore').css('max-height','90px');
$(this).parent().find('.more').removeClass('less').text('More');
} else {
$(this).parent().find('.text').addClass('showMore').css('max-height',$(this).parent().find('span').height());
$(this).parent().find('.more').addClass('less').text('Less');
}
});
* {transition: all 0.5s;}
.text {position:relative;width:400px;max-height:90px;overflow:hidden;}
.showMore {}
.text::after {
  content: "";
    position: absolute; bottom: 0; left: 0;
        box-shadow: inset 0 -26px 22px -17px #fff;
    height: 39px;
  z-index:99999;
  width:100%;
  opacity:1;
}
.showMore::after {opacity:0;}
.more {border-top:1px solid gray;width:400px;color:blue;cursor:pointer;}
.more.less {border-color:#fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<div class="text">
<span>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</span></div>
<div class="more">More</div>
</div>

(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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文