CSS Keyframes animation go back to initial state

2019-08-11 04:58发布

I'm calling an animation like this:

document.getElementById('banner').className = "changeColorToIndigo";

Then I've got the CSS property:

div.changeColorToIndigo {
animation-duration: 1s;
animation-name: changeColorToIndigo;
animation-fill-mode: forwards;
}

And the keyframes animation:

@keyframes changeColorToIndigo {
from {background-color: #00BBD2;}
to {background-color: #3F51B5;}
}

But the animation goes back to it's initial state after the animation has completed, why is that? I've set the fill mode to forwards and specified the to (100%) property.

1条回答
2楼-- · 2019-08-11 05:38

I've put your code in a Fiddle, and it works fine for me

HTML

<div id="banner"></div>

CSS

div{
    height: 40px;
    width: 40px;
    background-color: #00BBD2;
}

div.changeColorToIndigo {
    animation-name: changeColorToIndigo;
    animation-duration: 1s;
    animation-fill-mode: forwards;
}

@keyframes changeColorToIndigo {
    from {background-color: #00BBD2;}
    to {background-color: #3F51B5;}
}

jQuery

$(document).ready(function(){
    $('#banner').addClass('changeColorToIndigo');
})
查看更多
登录 后发表回答