这是基于您正在尝试做的 fiddle 的更新片段。
$(".heart").on('click touchstart', function(){
$(this).toggleClass('animating');
});
$(".heart").on('animationend', function(){
$(this).toggleClass('animating');
});
.heart {
float:left;
margin-top: -1em;
cursor: pointer;
height: 50px;
width: 50px;
background-image:url( 'https://abs.twimg.com/a/1446542199/img/t1/web_heart_animation.png');
background-position: left;
background-repeat:no-repeat;
background-size:2900%;
}
.heart:hover {
background-position:right;
}
.animating {
animation: heart-burst .8s steps(28) 1;
}
@keyframes heart-burst {
from {background-position:left;}
to { background-position:righ;}
}
<div class="heart-container">
<div class="heart"></div>
<p>TEXT</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我是一名优秀的程序员,十分优秀!