简体   繁体   English

React CSS 动画延迟属性不起作用

[英]React CSS animation-delay property does not work

just a quick question regarding animated elements in React.js - I have noticed that when trying to make use of the animation-delay property in CSS, that React does not seem to apply this rule at all for some reason.只是一个关于 React.js 中动画元素的快速问题——我注意到当尝试在 CSS 中使用动画延迟属性时,出于某种原因,React 似乎根本不应用此规则。

For example, I am trying to make a basic loading component, which just has a series of basic circles moving about in sequence, the CSS for which is below:例如,我正在尝试制作一个基本的加载组件,它只有一系列按顺序移动的基本圆圈,其 CSS 如下所示:

.--loaderContainer {
    position: relative;
    left: 50%;
    top: 50%;
    transform: translateX(-50%), translateY(-50%);
    width: 4rem;
    height: 4rem;
}

.--loaderContainer:nth-child(n) {
    width: 1rem;
    height: 1rem;
    border-radius: 9999px;
    margin: 0;
    padding: 0;
    position: absolute;
    animation-name: spin;
    animation-duration: 2s;
    animation-timing-function: ease;
    animation-iteration-count: infinite;
    animation-fill-mode: both;
}

.--loaderContainer:first-child {
    background-color: red;
    transform: rotate(270deg);
    animation-delay: -1.5s;
}

.--loaderContainer:nth-child(2) {
    background-color: blue;
    transform: rotate(180deg);
    animation-delay: -1s;
}

.--loaderContainer:nth-child(3) {
    background-color: green;
    transform: rotate(90deg);
    animation-delay: -0.5s;
}

.--loaderContainer:nth-child(4) {
    background-color: yellow;
}

@keyframes spin {
    0%,
    100% {
        transform: translate(0);
    }
    25% {
        transform: translate(160%);
    }
    50% {
        transform: translate(160%, 160%);
    }
    75% {
        transform: translate(0, 160%);
    }
}

As far as I can tell, this should stagger each of the child elements in sequence, performing the same animation but at different times.据我所知,这应该按顺序交错每个子元素,执行相同的 animation 但在不同的时间。 However, for some reason, React simply plays the animation, with no delay whatsoever and has all four elements animate at the exact same time.然而,出于某种原因,React 只是播放 animation,没有任何延迟,并且所有四个元素都在同一时间进行动画处理。

It seems like a really basic issue to be having and while I have looked up a variety of answers, I just can't seem to find any concrete solutions, so would be super appreciative for any help.这似乎是一个非常基本的问题,虽然我已经查找了各种答案,但我似乎找不到任何具体的解决方案,因此非常感谢您的帮助。 Thanks!谢谢!

If all four circles are seen, I think this animation should be working properly.如果看到所有四个圆圈,我认为这个 animation 应该可以正常工作。

The negative animation-delay values ensures that four circles start at different points of the animation, rather than putting an actual delay.animation-delay值确保四个圆圈从 animation 的不同点开始,而不是放置实际延迟。

More about animation-delay 更多关于animation-delay

Without it, they will start at same point and move together while stacking with each other, resulting in only one circle is seen (the last yellow circle since it's on top of stack).没有它,它们将从同一点开始并在彼此堆叠时一起移动,导致只能看到一个圆圈(最后一个yellow圆圈,因为它在堆叠顶部)。

Here is a basic example for testing the effect of animation-delay by toggling it on and off for all the circles, it can run with the "run code snippet" button below.这是一个基本示例,用于通过为所有圆圈打开和关闭animation-delay来测试它的效果,它可以使用下面的“运行代码片段”按钮运行。

Hope it will help.希望它会有所帮助。

 const btn = document.querySelector("button"); const loaders = document.querySelectorAll(".--loaderContainer"); btn.addEventListener("click", () => { loaders.forEach((loader) => loader.classList.toggle("no-delay")); btn.textContent = loaders[0].classList.contains("no-delay")? "turn delay on": "turn delay off"; });
 .--loaderContainer { position: relative; left: 50%; top: 50%; transform: translateX(-50%), translateY(-50%); width: 4rem; height: 4rem; }.--loaderContainer:nth-child(n) { width: 1rem; height: 1rem; border-radius: 9999px; margin: 0; padding: 0; position: absolute; animation-name: spin; animation-duration: 2s; animation-timing-function: ease; animation-iteration-count: infinite; animation-fill-mode: both; }.--loaderContainer:first-child { background-color: red; transform: rotate(270deg); animation-delay: -1.5s; }.--loaderContainer:nth-child(2) { background-color: blue; transform: rotate(180deg); animation-delay: -1s; }.--loaderContainer:nth-child(3) { background-color: green; transform: rotate(90deg); animation-delay: -0.5s; }.--loaderContainer:nth-child(4) { background-color: yellow; } @keyframes spin { 0%, 100% { transform: translate(0); } 25% { transform: translate(160%); } 50% { transform: translate(160%, 160%); } 75% { transform: translate(0, 160%); } } button { text-transform: uppercase; padding: 6px; } section { width: 100px; height: 100px; position: relative; } div.--loaderContainer.no-delay:nth-child(n) { animation-delay: 0s; opacity: 0.8; } body { background-color: #aaa; }
 <button>turn delay off</button> <section> <div class="--loaderContainer"></div> <div class="--loaderContainer"></div> <div class="--loaderContainer"></div> <div class="--loaderContainer"></div> </section>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM