效果图:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1 {
width: 100px;
height: 100px;
background: url(./image/1.png);
background-position: left center;
background-repeat: no-repeat;
}
.heartAnimation {
display: inline-block;
-webkit-animation-name: heartBlast;
animation-name: heartBlast;
-webkit-animation-duration: .8s;
animation-duration: .8s;
-webkit-animation-iteration-count: 1;
animation-iteration-count: 1;
-webkit-animation-timing-function: steps(28);
animation-timing-function: steps(28);
background-position: right;
}
@-webkit-keyframes heartBlast {
0% {
background-position: left;
}
100% {
background-position: right;
}
}
@keyframes heartBlast {
0% {
background-position: left;
}
100% {
background-position: right;
}
}
</style>
</head>
<body>
<div class="box1" onclick="change()"></div>
<script>
function change() {
var a = document.getElementsByClassName("box1")[0]
a.classList.add("heartAnimation")
}
</script>
</body>
</html>