【问题标题】:CSS transition doesn't animate when immediately changing size via JavaScript通过 JavaScript 立即更改大小时,CSS 过渡没有动画效果
【发布时间】:2017-05-15 10:34:59
【问题描述】:

在 JavaScript 中将元素的高度设置为 0,然后立即将其更改为特定值时,该元素的 CSS 过渡不起作用。

但是,通过将代码放置在 setTimeout() 内增加高度,即使延迟为 0,转换也可以正常工作,如下面的 sn-p 所示:

// Doesn't work:
document.getElementById("one").setAttribute("style", "height: 0px");
document.getElementById("one").setAttribute("style", "height: 200px");

// Works:
document.getElementById("two").setAttribute("style", "height: 0px");
setTimeout(function() {
	document.getElementById("two").setAttribute("style", "height: 200px");
}, 0);
div {
  display: inline-block;
  width: 200px;
  background-color: black;
  transition: height 1s;
}

#two {
  background-color: blue;
}
<div id="one">
</div>
<div id="two">
</div>

此行为在所有主要浏览器中都是一致的。这样做的问题是,有时,似乎存在某种滞后,这使得解决方法也没有动画效果。所以这似乎不是一个干净的解决方案。

是什么导致转换取消,我怎样才能彻底解决这个问题?

【问题讨论】:

  • setTimeout(..., 0) 并不是真正的0 超时,而是更像setTimeout(..., asSoonAsYouHaveTimeForThatButAtLeast(10))

标签: javascript css css-transitions css-animations


【解决方案1】:

最有可能的浏览器会优化过渡,并会在不到 16 毫秒的时间内合并更改(这将使您获得大约每秒 60 帧的刷新率)

因此解决方案是简单地包装样式更改嵌套的 RAF 调用(告诉浏览器在准备好时而不是在任意超时后进行动画处理)

window.requestAnimationFrame(function(){
document.getElementById("two").setAttribute("style", "height: 0px");
 window.requestAnimationFrame(function(){
    document.getElementById("two").setAttribute("style", "height:  200px");
  });
});

参考:https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame

【讨论】:

  • 你必须嵌套这两个函数调用,否则两个函数将在同一个动画帧上调用,因此样式将再次合并。
  • 嵌套 RAF 起到了作用。 @Tudor Ilisoi 一旦您更正了答案,我很乐意将您的答案标记为已接受。
  • @SimonMathewson,Tudor,你根本不需要等待,你可以force the browser into a render-cycle,就在你的代码中间。这可能很方便或相当昂贵,具体取决于您的使用方式。
  • @Thomas 谢谢,但是您建议的解决方案仅适用于 height 属性,并且正如您已经说过的那样,耗电量很大。 Tudor 建议的解决方案,经过您的更正,可以在更多情况下使用,并且看起来更直观。
【解决方案2】:

尝试将其添加到window.onload 事件中

window.addEventListener("load", init, false);

function init() {
    document.getElementById("one").style.height = "200px";
}

您还必须将 CSS 中的 #one 高度设置为 0

#one {
   height:0px;
}

【讨论】:

    【解决方案3】:
    //cache the object in a variable
    var one = document.getElementById("one");
    
    //do you really need to overwrite all stlyles defined on this object?
    //if not, prefer style-property over attribute
    //set animation-start
    one.style.height = 0;
    
    //force the browser to apply styles
    getComputedStyles(one);
    
    //apply the animation-target
    one.style.height = "200px";
    

    您不需要任何超时,但请注意,这会迫使浏览器进入渲染周期(在 JS 中可以做的最昂贵的事情之一)。

    所以不要在循环中使用它(或在多个节点上,一个接一个)。 但改为执行两个循环,首先设置所有起始值,强制浏览器进入渲染周期,然后应用所有目标值。

    【讨论】:

    • one.scrollHeight; 到底在做什么?
    • @SimonMathewson 它要求浏览器提供一个值,基本上是doNothingWith(one.scrollHeight)。为了正确响应这个请求,浏览器必须首先应用挂起的样式更改(因为style.height = 0 可能会更改scrollHeight)。这基本上是一个渲染周期(尽管它可能不会刷新屏幕)。现在style.height = '200px' 是标记为要应用的下一个 渲染周期的待定样式,因此它们不会被合并。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 2013-01-17
    相关资源
    最近更新 更多