【问题标题】:Triggering two different animations onClick?点击触发两个不同的动画?
【发布时间】:2021-07-06 14:29:10
【问题描述】:

我试图通过使用 javascript 添加和删除具有两个不同 css 动画的两个不同 css 类来触发两个不同的动画。但是,div 不会保留以前的动画状态,由于我设计 UI 的方式,我需要它来这样做。我在 animations 属性中使用了 forwards,但是 div 在单击时执行动画,然后返回到以前的状态,然后执行另一个动画。

const projects = document.getElementById('projects')

projects.addEventListener('click', () => {
    if(projects.classList.contains('projects-animation') == true){
        projects.classList.remove('projects-animation')
        projects.classList.add('projects-animation2')
    }
    else{
        console.log('animation2')
        console.log(projects.classList)
        projects.classList.remove('projects-animation2')
        projects.classList.add('projects-animation')
    }
})
.projects-container{
    position: relative;
    z-index: 1;
    display: flex;
    align-items: center;
    grid-area: projects;
    padding: 20px;
    font-size: 100px;
    background-color: hsl(4, 7%, 45%);
    cursor: pointer;
}

.projects{
    position: relative;
    height: fit-content;
}

.projects-animation{
    animation: projects 1s ease-in-out backwards;
}

.projects-animation2{
    animation: projects2 1s ease-in-out backwards;
}

@keyframes projects {
    100%{
        transform: translate(50%,-250%);
    }
}

@keyframes projects2 {
    100%{
        transform: translate(-50%, 250%);
    }
}
<div class="projects-container">
     <div id = "projects" class="projects">Projects</div>
</div>

【问题讨论】:

  • 在两个值之间加一个逗号

标签: javascript css animation


【解决方案1】:

请检查这个,

1:需要添加0%作为第二个动画的起点, 2:我使用较小的值作为较大的值,将子 div 移出视口,如果您想保留这些值,则使父 div 变大或相应定位

    const projects = document.getElementById('projects')

    projects.addEventListener('click', () => {
        if(projects.classList.contains('projects-animation') == true){
            projects.classList.remove('projects-animation')
            projects.classList.add('projects-animation2')
        }
        else{
            console.log('animation2')
            console.log(projects.classList)
            projects.classList.remove('projects-animation2')
            projects.classList.add('projects-animation')
        }
    })
    .projects-container{
        position: relative;
        z-index: 1;
        display: flex;
        align-items: center;
        grid-area: projects;
        padding: 20px;
        font-size: 100px;
        background-color: hsl(4, 7%, 45%);
        cursor: pointer;
    }

    .projects{
        position: relative;
        height: fit-content;
    }

    .projects-animation{
        animation: projects 1s ease-in-out forwards;
    }

    .projects-animation2{
        animation: projects2 1s ease-in-out forwards;
    }

    @keyframes projects {
        100%{
            transform: translate(10%,-10%);
        }
    }

    @keyframes projects2 {
        0%{
            transform: translate(10%,-10%);
        }
        100%{
            transform: translate(0%, 0%);
        }
    }
    <div class="projects-container">
         <div id = "projects" class="projects">Projects</div>
    </div>

【讨论】:

    猜你喜欢
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2014-07-27
    相关资源
    最近更新 更多