css 过渡动画高度变化问题
问题:
在给定的 js bin 中,当 <p> 标签显示后,.box 元素的高度会被自动撑起,但没有动画效果。如何实现点击按钮后,.box 高度慢慢变化的动画效果?
答案:
css 动画不支持 height: auto,因此需要使用 javascript 获取 .box 元素的实际高度,并在点击按钮时切换高度,从而实现动画效果。
具体实现步骤:
.box { background-color: blue; overflow: hidden; transition: all 1s; }
const autoh = $('.box').height();
$('.box').height(0);
$('.button').click(function() { $('.box').height(h ^= autoH); });
通过这种方式,就可以实现点击按钮后,.box 高度慢慢变化的动画效果。