【发布时间】:2023-12-24 08:08:01
【问题描述】:
我正在尝试在应用类后为元素的高度设置动画,这是简化的代码:
HTML
<div class="section">
<div class="panel">
<a href="#" class="toggle">Click</a>
<div class="panel-content">
Some content...
</div>
</div>
</div>
CSS
.section {
position: relative;
width: 500px;
height: 200px;
margin: 100px auto;
background: #ccc;
}
.panel {
width: 65%;
position: absolute;
left: 0;
bottom: 0;
}
.toggle {
display: inline-block;
height: 15px;
background: #ddd;
}
.panel-content {
max-height: 0;
overflow: hidden;
transition: max-height 1s;
}
.active .panel-content {
max-height: 9999px;
}
JS
$(function() {
$('.toggle').on('click', function (e) {
e.preventDefault();
$(this).closest('.panel').toggleClass('active');
});
});
当我点击.toggle 链接时,.panel 元素上设置了一个active 类来为.panel-content 高度设置动画,但是当第一次添加该类时,显示的内容没有动画,当它被删除时元素需要一秒钟(过渡的持续时间)开始动画。你可以在这里看到一个现场演示:http://codepen.io/javiervd/pen/bLhBa
我也尝试过使用 position 和 overflow 属性,但我无法让它发挥作用,也许还有另一种方法可以达到同样的效果?
提前致谢。
【问题讨论】:
标签: css height css-transitions animated