问题
what is wrong with my css,the bounce animation just doesn`t happen??? I want an image to bounce on click according to my css animation which is not happening
HTML CODE::
<div class="hair">
<img src="images/single.png" id="hair1" class="hair_animate" width="13" height="40" onclick="bounce();" >
</div>
Css Code:
.hair{
position: absolute;
top: 500px;
}
@-webkit-keyframes bounce {
0%, 20%, 50%, 80%, 100% {-webkit-transform: translateY(0);}
40% {-webkit-transform: translateY(-60px);}
60% {-webkit-transform: translateY(-35px);}
}
.bounce_css
{
-webkit-animation-name: bounce;
}
Javascript code:
function bounce(){
alert("reached")
document.getElementById('hair1').style.WebkitAnimationName = 'bounce_css';
}
回答1:
I would expect your code to look more like this
CSS:
.animated {
-webkit-animation-fill-mode:both;
-moz-animation-fill-mode:both;
-ms-animation-fill-mode:both;
-o-animation-fill-mode:both;
animation-fill-mode:both;
-webkit-animation-duration:1s;
-moz-animation-duration:1s;
-ms-animation-duration:1s;
-o-animation-duration:1s;
animation-duration:1s;
}
@-webkit-keyframes bounce {
0%, 20%, 50%, 80%, 100% {
-webkit-transform: translateY(0);
}
40% {
-webkit-transform: translateY(-60px);
}
60% {
-webkit-transform: translateY(-35px);
}
}
@-moz-keyframes bounce {
0%, 20%, 50%, 80%, 100% {
-moz-transform: translateY(0);
}
40% {
-moz-transform: translateY(-60px);
}
60% {
-moz-transform: translateY(-35px);
}
}
@-o-keyframes bounce {
0%, 20%, 50%, 80%, 100% {
-o-transform: translateY(0);
}
40% {
-o-transform: translateY(-60px);
}
60% {
-o-transform: translateY(-35px);
}
}
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-60px);
}
60% {
transform: translateY(-35px);
}
}
.bounce_css {
-webkit-animation-name: bounce;
-moz-animation-name: bounce;
-o-animation-name: bounce;
animation-name: bounce;
}
JavaScript:
function bounce() {
document.getElementById('hair1').className = "animated bounce_css";
}
Running Example:
http://jsfiddle.net/GALU5/1/
回答2:
Try:
function bounce(){
alert("reached")
document.getElementById('hair1').classList.add('bounce_css');
}
Or:
function bounce(){
alert("reached")
document.getElementById('hair1').style.WebkitAnimationName = 'bounce';
}
来源:https://stackoverflow.com/questions/16917457/css-bounce-and-add-class-in-javascript