javascript - webkit 中的 CSS 动画在自动之前变为零

标签 javascript css animation webkit

我正在处理一个元素,我需要将一个 div 设置为指定高度的动画,然后将其 height 设置为 auto。奇怪的是,当div的高度设置为auto时,高度动画为height,然后跳转到auto。我在 Safari 和 Chrome 中观察到了这种行为。

这是我的测试代码:

HTML:

<div id="a" style="height:0px;"><div style="height:100px;"></div></div>

CSS:

#a{
    -webkit-transition: all .5s ease-out;
    -moz-transition: all .5s ease-out;
    -o-transition: all .5s ease-out;
    transition: all .5s ease-out;
    width:100px;
    background:blue;
}

JavaScript:

window.setTimeout(function(){
    document.getElementById("a").style.height = "100px";
}, 10);

window.setTimeout(function(){
    //The auto height will be 100px. The div will already be at this... But it animates to 0px and then goes to auto. Strange right!?!?
    document.getElementById("a").style.height = "auto";
}, 2000);

FIDDLE

我该如何解决这个问题?谢谢。 如果您投反对票,请告诉我原因,以便我改进这个问题。

最佳答案

不幸的是,此时您无法使用 CSS3 动画将动画设置为自动。另一种方法是尝试为 transform 属性设置动画并从 transform: scale(0)transform: scale(1),但是你不会有预期的效果。这是一个棘手的主题,很多人都试图处理它并绕过它!

关于javascript - webkit 中的 CSS 动画在自动之前变为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26622126/

相关文章:

javascript - 在 webpack 中包含 jquery-sparkline

javascript - 使用 angular.element 函数动态获取当前焦点

html - 在 CSS 中创建一个 Googlelike 的输入

html - Android webview单选按钮和复选框样式

javascript - 分页替代方案

javascript - 如何在NEST Js中使用全局拦截器中的服务

javascript - D3 : Animate circle along border of country on spinning globe

jquery - .toggle 来自 <a> 的 div 不工作

python - imshow.set_data() 不适用于 FuncAnimation

Javascript - 在页面加载时更改所有动画播放状态