问题
I have a DIV which changes bgcolor to blue when :hover. Button click adds class which animates bgcolor to green. But now :hover wont work (bgcolor wont change to blue).
Why?
http://jsfiddle.net/EstSiim/me7t055c/
HTML:
<div class="box"></div>
<button class="animate-btn">Animate</button>
CSS:
.box {
background-color: red;
width: 200px;
height: 200px;
}
.box:hover {
background-color: blue;
}
.trigger-bg-change {
-webkit-animation: bg-change 1s ease 0 1 forwards;
animation: bg-change 1s ease 0 1 forwards;
}
@-webkit-keyframes bg-change {
0% {background-color:red;}
100% {background-color:green;}
}
@-keyframes bg-change {
0% {background-color:red;}
100% {background-color:green;}
}
JS:
$(function() {
$('.animate-btn').on('click', function() {
console.log('hey');
$('.box').addClass('trigger-bg-change');
});
});
回答1:
DEMO
You can use the following CSS rules for .trigger-bg-change
instead:
.trigger-bg-change {
background-color: green;
-webkit-animation: bg-change 1s ease 0 1 none;
animation: bg-change 1s ease 0 1 none;
}
By changing the animation-fill-mode property from forwards
to none
, the animation will not apply the styles to to element after it has executed, so the rules for .box:hover
will not get overridden.
回答2:
DEMO 1 (With JavaScript - See code below)
DEMO 2 (Without JavaScript - See code below)
JavaScript Solution:
HTML
<div class="box-init"></div>
<button class="run-btn">Run Animation</button>
CSS
.box-init, .box-post {
width: 200px;
height: 200px;
margin-bottom:10px;
}
.box-init{
background-color: red;
}
.box-post{
background-color: green;
}
.box-post:hover{
background-color:blue;
}
.box-transition{
transition-property: background-color;
transition-duration: 0.5s;
transition-timing-function: ease;
transition-delay: 0s;
}
.box-animation{
/*Animation properties*/
animation-name: bg-change;
animation-duration: 0.5s; /* <-- ANIMATION DURATION IS 0.5 SECONDS!! */
animation-iteration-count: initial;
animation-direction: initial;
animation-timing-function: ease-in;
animation-fill-mode: forwards;
}
@keyframes bg-change {
100% {background-color:green;}
}
JavaScript
const runBtn = document.querySelector('.run-btn');
const box = document.querySelector('.box-init');
runBtn.addEventListener('click', ()=>{
box.classList.add("box-animation"); // animation duration is 0.5 seconds
setTimeout(() => { // so after 0.6 seconds we will remove the animation
box.classList.add("box-post");
box.classList.remove("box-init");
box.classList.remove("box-animation");
box.classList.add("box-transition");
}, 600); //0.6 seconds
});
Alternative Solution (Without using JavaScript):
HTML
<div class="box"></div>
CSS
.box {
background-color: red;
width: 200px;
height: 200px;
margin-bottom:10px;
/*Animation properties*/
animation-name: bg-change;
animation-duration: 0.5s;
animation-iteration-count: initial;
animation-direction: initial;
animation-timing-function: ease-in;
animation-fill-mode: forwards;
}
@keyframes bg-change {
0% {background-color:red;}
100% {background-color:green;}
}
.box:hover{
/*Animation properties*/
animation-name: hover-animation;
animation-duration: 0.5s;
animation-iteration-count: initial;
animation-direction: initial;
animation-timing-function: ease-in;
animation-fill-mode: forwards;
}
@keyframes hover-animation {
0% {background-color: green;}
100% {background-color:blue;}
}
来源:https://stackoverflow.com/questions/25260281/css3-hover-wont-work-after-animation