我正在尝试了解网络动画标准及其polyfill,因为我看到它在 Angular 动画库中运行良好(您将动画结束值设置为 '*' ,这将成为 div 大小的 100%,但是使用特殊的 Angular Animations DSL)。
我以为我会从一些简单的事情开始,所以我想做的就是将 div 从 0 高度扩展到“自动”。我知道有数千种其他方法可以做到这一点,但我试图在这段代码中使用 web-animations-js
下面的代码(类似于MDN 示例)导致 div 直接扩展为“自动”,但延迟 1 秒后,而我希望它顺利扩展。
let formDiv = document.querySelector("#new-present-form");
formDiv.animate([
{ height: 0},
{ height: 'auto'}
], {
easing: 'ease-in-out',
duration: 1000,
fill: 'forwards' // ensures menu stays open at end of animation
})
相比之下这个
formDiv.animate({
height: [0, '100%'],
easing: 'ease-in-out'
}, {
duration: 1000,
fill: 'forwards' // ensures menu stays open at end of animation
})
导致 div 立即展开,但又没有平滑过渡。
这确实提供了平滑的过渡,但需要仔细选择值来替换“300px”,而这正是我想要避免的。
formDiv.animate([
{ "height": '0px', offset: 0},
{ "height": '300px', offset: 1}
], {
duration: 1000,
iterations: 1,
fill: 'forwards' // ensure form stays open at end of animation
})