但如果想给一个 height:auto 的块级元素的高度添加 CSS3 动画时,该怎么办呢?
从 MDN 的可以查到 CSS 支持动画的属性中的 height 属性如下:
height :yes, as a length, percentage or calc(); // 当 height 的值是 length,百分比或 calc() 时支持 CSS3 过渡。
所以当元素 height : auto 时,是不支持 CSS3 动画的。
除了通过 JS 获取精确的 height 值的方法外,其实我们可以使用 max-height 代替 height。
只要我们设置一个肯定比元素自增长大的高度值就可以了。当然,因为是根据 max-height 值进行过渡效果,所以最好不要大得离谱,否则动画效果不理想。
1 2 3 4 5 6 *{ 7 margin: 0; 8 padding:0; 9 }10 div{11 12 display: inline-block;13 overflow: hidden;14 background-color: orange;15 max-height: 20px;16 -webkit-transition: max-height 1s;17 transition: max-height 1s;18 }19 div:hover{20 max-height:200px;21 }22 23 24 2526
我不是height,是max-height
27我不是height,是max-height
28我不是height,是max-height
29我不是height,是max-height
30我不是height,是max-height
31我不是height,是max-height
32查看更多关于如何为什么不定高度的元素添加CSS3transition_html/css_WEB-ITnose的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did108412