gpt4 book ai didi

css 动画一起结束,即使它们在不同时间开始

转载 作者:行者123 更新时间:2023-11-28 09:43:00 25 4
gpt4 key购买 nike

我有几个相同类型的元素,我希望它们共享相同的 css 动画,但我希望它们在不同的时间开始/结束动画。

Codepen for the following code

html:

<div class="container">
<div class="box hidden"></div>
</div>
<div class="container">
<div class="box hidden"></div>
</div>
<div class="container">
<div class="box hidden"></div>
</div>

CSS:

.container {
width: 100px;
height: 100px;
margin-bottom: 20px;
}

.box {
width: 100%;
height: 100%;
}

.box.hidden {
visibility: hidden;
}

.box {
animation: growIn 1s;
animation-timing-function: cubic-bezier(.46,.13,.99,.83);
transition: all .2s cubic-bezier(0.215, 0.61, 0.355, 1);
}

.container:first-child .box {
background-color: green;
}

.container:nth-child(2) .box {
background-color: orange;
}

.container:nth-child(3) .box {
background-color: red;
}

@keyframes growIn {
from {
transform: scale(0);
}
to {
transform: scale(1);
}
}

box 元素开始时是隐藏的,然后使用 javascript 我在不同的时间从不同的框中删除了这个类名:

const boxes = document.querySelectorAll(".box");
boxes.forEach(box => {
setTimeout(() => box.classList.remove("hidden"), Math.random() * 1000);
});

发生的情况是所有 3 个盒子同时结束它们的动画。动画确实在不同的时间开始,但都一起结束。

这是为什么?
如果我做同样的事情,但添加一个类名而不是删除它(为了使动画开始),那么它的行为就像我想要的那样。
有任何想法吗?谢谢。

最佳答案

因为所有的动画都已经同时开始。使用 visibility:hidden 不会阻止动画开始并使其在元素可见时稍后开始。例如,不透明度也会发生同样的事情:

const boxes = document.querySelectorAll(".box");
boxes.forEach(box => {
setTimeout(() => box.classList.remove("hidden"), Math.random() * 5000);
});
.container {
width: 100px;
height: 100px;
margin-bottom: 20px;
}

.box {
width: 100%;
height: 100%;
}

.box.hidden {
opacity: 0.1;
}

.box {
animation: growIn 5s;
animation-timing-function: cubic-bezier(.46, .13, .99, .83);
transition: all .2s cubic-bezier(0.215, 0.61, 0.355, 1);
}

.container:first-child .box {
background-color: green;
}

.container:nth-child(2) .box {
background-color: orange;
}

.container:nth-child(3) .box {
background-color: red;
}

@keyframes growIn {
from {
transform: scale(0);
}
to {
transform: scale(1);
}
}
<div class="container">
<div class="box hidden"></div>
</div>
<div class="container">
<div class="box hidden"></div>
</div>
<div class="container">
<div class="box hidden"></div>
</div>

如果您改为使用 display 属性,您可以看到您正在寻找的行为:

const boxes = document.querySelectorAll(".box");
boxes.forEach(box => {
setTimeout(() => box.classList.remove("hidden"), Math.random() * 3000);
});
.container {
width: 100px;
height: 100px;
margin-bottom: 20px;
}

.box {
width: 100%;
height: 100%;
}

.box.hidden {
display:none;
}

.box {
animation: growIn 1s;
animation-timing-function: cubic-bezier(.46, .13, .99, .83);
transition: all .2s cubic-bezier(0.215, 0.61, 0.355, 1);
}

.container:first-child .box {
background-color: green;
}

.container:nth-child(2) .box {
background-color: orange;
}

.container:nth-child(3) .box {
background-color: red;
}

@keyframes growIn {
from {
transform: scale(0);
}
to {
transform: scale(1);
}
}
<div class="container">
<div class="box hidden"></div>
</div>
<div class="container">
<div class="box hidden"></div>
</div>
<div class="container">
<div class="box hidden"></div>
</div>


来自specification :

The 'visibility' property specifies whether the boxes generated by an element are rendered.

Invisible boxes still affect layout (set the 'display' property to 'none' to suppress box generation altogether).

因此,与使用 display 时不同,使用 visibility 时,框总是生成。

如果我们检查 specification与动画相关,我们会发现:

Setting the display property to none will terminate any running animation applied to the element and its descendants. If an element has a display of none, updating display to a value other than none will start all animations applied to the element by the animation-name property, as well as all animations applied to descendants with display other than none.

关于css 动画一起结束,即使它们在不同时间开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51114281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com