css过渡max-height回到0不起作用

时间:2016-11-28 18:13:35

标签: css css-transitions

我有一个简单的div,其高度和最大高度设置为10px。当我将鼠标悬停在它上面时,它应该扩展到div的整个高度,当我离开时它应该缩小到10px。

但是,当我取消切换时,下面的代码将无法顺利转回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 个答案:

答案 0 :(得分:9)

从代码中删除height:10px;。它会使高度为10px的高优先级并使溢出隐藏。这就是动画无效的原因。有关max-height属性的更多详情,请点击this link

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;