CSS过渡效果的max-height属性无法返回0的问题不起作用。

7
我有一个简单的div,其高度和最大高度都设置为10像素。当我悬停在它上面时,它应该扩展到整个div的高度,当我离开时,它应该缩回到10像素。
但是当我取消悬停时,以下代码无法平稳地过渡回0。
HTML
<div class="animate">
   Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory  ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
</div>

CSS

.animate{
  font-size:20px;
  height : 10px;
  max-height:10px;
  width: 100px;
  overflow:hidden;
  -webkit-transition: all 0.5s ease-in-out;
  -moz-transition: all 0.5s ease-in-out;
  -ms-transition: all 0.5s ease-in-out;
  -o-transition: all 0.5s ease-in-out;
  transition: all 0.5s ease-in-out;
}
.animate:hover{
  height:auto;
  max-height:1000px;
}

https://jsfiddle.net/3hfxg6he/2/

1个回答

16
从你的代码中删除height:10px;。它会使高度成为10像素并具有高优先级,并且会使溢出隐藏。这就是动画不起作用的原因。有关max-height属性的更多详细信息,请参阅此链接


.animate{
  font-size:20px;
  max-height:10px;
  width: 100px;
  overflow:hidden;
  -webkit-transition: all 0.5s ease-in-out;
  -moz-transition: all 0.5s ease-in-out;
  -ms-transition: all 0.5s ease-in-out;
  -o-transition: all 0.5s ease-in-out;
  transition: all 0.5s ease-in-out;
}
.animate:hover{
  height:auto;
  max-height:1000px;
}
<div class="animate">
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory  ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
</div>


1
它不会使div动画化,因为过渡不知道应该动画多少。我们应该给出一些“max-height”值。 - jafarbtech
如果这真的有帮助,请接受并点赞。这将有助于为像您这样的人寻找解决方案。 - jafarbtech
2
我不能接受,因为这不是我的问题;-) 但我已经点赞了。:-D - Rounin
2
max-height 应该覆盖 height,所以不确定您所说的高度具有“高优先级”的含义。https://developer.mozilla.org/zh-CN/docs/Web/CSS/max-height - Michael Benjamin
2
@Michael_B 但是覆盖只会在高度超过max-height值时发生。但是这里他只给了最小值10px,低于:hovermax-height属性。这就是为什么我们必须将其删除的原因。 - jafarbtech
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接