简体   繁体   English

如何将 animation 延迟仅应用于一个 animation 和 CSS 和 javascript?

[英]How to apply an animation delay to only one animation with CSS and javascript?

I am new to css and javascript. I am trying to animate the spans within an h2 to fall from the top of the page on load and then on hover to have a bouncy effect.我是 css 和 javascript 的新手。我正在尝试为 h2 中的跨度设置动画,使其在加载时从页面顶部落下,然后在 hover 上设置动画以产生弹性效果。 I was able to make them happen both BUT now the problem is that the animation delay I intended for the fall animation are applying to the bounce animation as well.我能够让它们同时发生,但现在的问题是我打算为秋季 animation 设置的 animation 延迟也适用于反弹 animation。 When I add the animation name to the animation delay all the letter fall at the same time.当我将 animation 名称添加到 animation 时,所有字母同时落下。 What am I missing?我错过了什么?

I tried specifying the animation name for the animation delay but it didn't work.我尝试为 animation 延迟指定 animation 名称,但没有成功。 When I add the animation name to the animation delay all the letter fall at the same time.当我将 animation 名称添加到 animation 时,所有字母同时落下。 What am I missing?.我错过了什么? And I also tried to change the animation delay in JS after the first animation happens but wasn't able to figure out.而且我还尝试在第一个 animation 发生后更改 JS 中的 animation 延迟但无法弄清楚。

This is my html这是我的 html

 <h2 class="test">
                    <span class="q">T</span><span class="q">a</span><span class="q">d</span><span
                        class="q">a</span><span class="q">a</span><span class="q">k</span><span class="q">i</span>
                </h2>
                <h2 class="test2">
                    <span class="q2">K</span><span class="q2">u</span><span class="q2">w</span><span
                        class="q2">a</span><span class="q2">y</span><span class="q2">a</span><span
                        class="q2">m</span><span class="q2">a</span>
                </h2>

This is the CSS这是CSS

span {
    color: black;
    font-size: 1em;
    display: flex;
    margin-bottom: 0;
    padding-bottom: 0;
}

.onLoadAnimation {
    position: relative;
    transform: translateY(-100vh);
    animation: fall 1s forwards;
    animation-iteration-count: 1;
}

@keyframes fall {
    100% {
        transform: translateY(0);
    }
}

.test span:nth-child(2) {
    animation-delay: 0.1s;
}

.test span:nth-child(3) {
    animation-delay: 0.2s;
}

.test span:nth-child(4) {
    animation-delay: 0.3s;
}

.test span:nth-child(5) {
    animation-delay: 0.4s;
}

.test span:nth-child(6) {
    animation-delay: 0.5s;
}

.test span:nth-child(7) {
    animation-delay: 0.6s;
}

.test2 span:nth-child(1) {
    animation-delay: 0.1s;
}

.test2 span:nth-child(2) {
    animation-delay: 0.12s;
}

.test2 span:nth-child(3) {
    animation-delay: 0.3s;
}

.test2 span:nth-child(4) {
    animation-delay: 0.4s;
}

.test2 span:nth-child(5) {
    animation-delay: 0.5s;
}

.test2 span:nth-child(6) {
    animation-delay: 0.6s;
}

.test2 span:nth-child(7) {
    animation-delay: 0.7s;
}

.test2 span:nth-child(8) {
    animation-delay: 0.8s;
}

.spanHoverEffect {
    color: #0f4c5c;
    animation: animate 0.6s;
}

@keyframes animate {
    25% {
        transform: scale(0.8, 1.3);
    }

    50% {
        transform: scale(1.1, 0.8);
    }

    75% {
        transform: scale(1.1, 0.8);
    }
}

This is the JS这是JS

let letters = document.getElementsByClassName("q");
let lettersTwo = document.getElementsByClassName("q2");

window.onload = () => {
    for (l of letters) {
        l.classList.toggle('onLoadAnimation');
        l.addEventListener('mouseover', function () {
            this.classList
                .remove('onLoadAnimation')
                .add('spanHoverEffect')
        });
        l.addEventListener('mouseout', function () {
            this.classList
                .remove('onLoadAnimation')
                .remove('spanHoverEffect')
        });
    }

    for (l of lettersTwo) {
        l.classList.toggle('onLoadAnimation');
        l.addEventListener('mouseover', function () {
            this.classList
                .remove('onLoadAnimation')
                .add('spanHoverEffect')
        });
        l.addEventListener('mouseout', function () {
            this.classList
                .remove('onLoadAnimation')
                .remove('spanHoverEffect')
        });
    }
};

In this fiddle , I made two changes.这个小提琴中,我做了两处改动。 The letters fall in sequence in Chrome, Firefox and Edge and the animation delay is not present when moused over in any of those browsers.这些字母在 Chrome、Firefox 和 Edge 中按顺序排列,并且在任何这些浏览器中将鼠标悬停在上面时都不存在 animation 延迟。

I added the onLoadAnimation class to the CSS for each letter.我为每个字母将 onLoadAnimation class 添加到 CSS。

.test .onLoadAnimation:nth-child(2) {
    animation-delay: 0.1s;
}

And I removed the chained changes to classList, which is not something that classList supports in any of the browsers mentioned above.我删除了对 classList 的链接更改,这不是 classList 在上述任何浏览器中支持的内容。

l.classList.remove('onLoadAnimation')
l.classList.add('spanHoverEffect')

The reason I mention web browsers is that "this.classList.remove('onLoadAnimation').add('spanHoverEffect')" causes an error in all of those browsers, because remove returns undefined, so I am wondering if you are using a less-used browser that may work differently.我提到 web 浏览器的原因是“this.classList.remove('onLoadAnimation').add('spanHoverEffect')”在所有这些浏览器中导致错误,因为 remove 返回未定义,所以我想知道你是否使用可能工作方式不同的较少使用的浏览器。

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

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