简体   繁体   English

如何使div的高度与内容和宽度平滑过渡?

[英]How can I make the height of a div transition smoothly with content and width?

I have a div that functions as an information box, giving extra information on the item it is associated with. 我有一个用作信息框的div ,它提供了与之关联的项目的更多信息。 Now this div might contain any amount of content, so I cannot fix the height in the css. 现在,该div可能包含任意数量的内容,因此无法固定CSS的高度。 This would be fine, but I would also like this div to have a nice transition when the user opens it, and this is where I run into a problem. 很好,但是我也希望该div在用户打开它时有一个很好的过渡,这就是我遇到问题的地方。 I have all the transitions working correctly, and they are here (+ open/closed css) 我的所有转换都正常工作,它们在这里(+打开/关闭CSS)

#info.hidden {
  opacity:0;
  width:0px;
  height:10px;

  transition: width .5s ease-in-out, height .5s ease-in-out, opacity .5s ease-in-out, visibility 0s linear .5s;
  /* with -moz- & -webkit- */
}

#info.visible {
  width:150px;

  transition: width .5s ease-in-out, height .5s ease-in-out, opacity .5s ease-in-out;
  /* with -moz- & -webkit- */
}

The problem I have is that the small width during the transition causes the text to wrap, extending the height of the box and not having the desired effect. 我的问题是过渡期间的小宽度导致文本换行,从而延长了框的高度,并且没有达到预期的效果。 Ideally, the height would expand smoothly from 10px to the height of the text, but instead it follows the bottom of the text all the way through the transition. 理想情况下,高度可以从10px平滑扩展到文本的高度,但在整个过渡过程中,它始终沿文本的底部延伸。 Also on close the height jumps straight to 10px. 同样在关闭时,高度直接跳到10px。

This problem can be seen here: http://jsfiddle.net/Daniel300/1nhs9w78/ . 可以在这里看到此问题: http : //jsfiddle.net/Daniel300/1nhs9w78/

I have tried various things, including adding a wrapper div to the text, hoping that the wrapper div would overflow past the bottom (or the side) of the div during the transition. 我尝试了各种方法,包括在文本中添加包装器div,希望包装器div在过渡期间溢出div的底部(或侧面)。 I also messed about with the display and overflow properties, but nothing seemed to work. 我也搞砸了displayoverflow属性,但似乎没有任何效果。

Thanks for any help! 谢谢你的帮助!

I found a way to do this that is not quite a valid solution, but is a workaround. 我找到了一种解决方法,它不是一个有效的解决方案,但是是一种解决方法。 It seems to be the best I can do for now. 目前看来,这是我能做的最好的事情。

Firstly, adding white-space: nowrap; 首先,添加white-space: nowrap; to the div prevented the text from, well, wrapping, and extending the div. div阻止了文本的自动换行和扩展。

Secondly, I found this post which I saw before but I thought that it didn't work (possibly I set the value too high). 其次,我找到了我以前看过的帖子 ,但我认为它不起作用(可能我将值设置得太高)。 Essentially, you can't transition from fixed value to auto (which is what I was trying to do). 本质上,您不能从固定值转换为自动值(这正是我试图做的)。 Therefore you must instead transition the max-height up to a value it will never reach. 因此,您必须改为将最大高度转换为一个永远不会达到的值。 Obviously I can't strictly speaking do this as per my question, but I can set it to a reasonable maximum value. 显然,我不能严格按照我的问题进行此操作,但可以将其设置为合理的最大值。

Here is my new code: https://jsfiddle.net/Daniel300/mfegxzuc/ . 这是我的新代码: https : //jsfiddle.net/Daniel300/mfegxzuc/

Not perfect, but possibly as good as I'm going to get with CSS. 不完美,但可能和我要使用CSS一样好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM