我爱编程

使用CSS3 animation实现gif动图的暂停和播放

2018-05-24  本文已影响0人  记忆是条狗

CSS代码:

.love {
    display: block;
    width: 100px; height: 100px;
    background: url(web_heart_animation.png) 0 0 no-repeat;
    background-size: 2900%;
    animation: heart-burst steps(28) 0.8s infinite both;
}
.stop {
    animation-play-state: paused;
}
@keyframes heart-burst {
  0% {
    background-position: 0%;
  }
  100% {
    background-position: 100%;
  }
}

HTML代码:

<i id="testImg" class="love"></i>
<p><input type="button" id="testBtn" value="暂停"></p>
JS代码:
var image = document.getElementById("testImg"), 
    button = document.getElementById("testBtn");
    
if (image.classList && image && button) {
    button.onclick = function() {
        if (this.value == '暂停') {
            image.classList.add('stop');
            this.value = '播放';
        } else {
            image.classList.remove('stop');
            this.value = '暂停';
        }
    };
}
web_heart_animation.png
上一篇下一篇

猜你喜欢

热点阅读