Nir*_*dar 8 css css-animations
我有一个动态高度的div,我想在高度增长时使用动画。问题是我无法设置 div 的高度(它必须是动态的),所以没有什么可以动画的。
.text {
transition: all .3s ease-in-out;
min-height: 20px;
max-height: 200px;
overflow: auto;
}
.max {
transition: all .3s ease-in-out;
height: 200px;
min-height: 200px;
}
Run Code Online (Sandbox Code Playgroud)
https://jsfiddle.net/abk7yu34/5/
请注意,缩小动画有效,因为 div 具有最小高度。
有没有办法在纯 CSS 中解决这个问题?
删除height: 200px;,这样你就有了展开和折叠的动画。
此外,使用以下内容在新行上添加动画:
@keyframes lineInserted {
from {
height: 0;
}
to {
height: 20px; /* your line height here */
}
}
div[contenteditable] > div {
animation-duration: 0.3s;
animation-name: lineInserted;
transition: height 0.3s;
}
Run Code Online (Sandbox Code Playgroud)
@keyframes lineInserted {
from {
height: 0;
}
to {
height: 20px; /* your line height here */
}
}
div[contenteditable] > div {
animation-duration: 0.3s;
animation-name: lineInserted;
transition: height 0.3s;
}
Run Code Online (Sandbox Code Playgroud)
document.querySelector('#button').addEventListener('click', function() {
document.querySelector('.text').classList.toggle('max');
});Run Code Online (Sandbox Code Playgroud)
.text {
background: green;
color: #fff;
transition: all .3s ease-in-out;
min-height: 20px;
max-height: 200px;
overflow: auto;
}
.max {
transition: all .3s ease-in-out;
min-height: 200px;
}
@keyframes lineInserted {
from {
height: 0;
}
to {
height: 20px; /* your line height here */
}
}
div[contenteditable] > div {
animation-duration: 0.3s;
animation-name: lineInserted;
transition: height 0.3s;
}Run Code Online (Sandbox Code Playgroud)