css 动画播放暂停

animation css

<html>
<head>
  <style>
    .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%;
      }
    }
  </style>
</head>

<body>
  <i id="testImg" class="love"></i>
  <p>
    <input type="button" id="testBtn" value="暂停">
  </p>
  <script>
    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 = '暂停';
        }
      };
    }
  </script>

</body>
</html>

web_heart_animation.png
暂停状态
播放状态

代码实现来源于 记忆是条狗

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,039评论 3 119
  • 虚荣 真实 真实的虚荣
    空语迟阅读 780评论 0 0
  • 1.使用# webpack版本问题### 目前有两个主版本,一个是在 master 主干的稳定版,一个是在 web...
    qwerer阅读 2,515评论 0 0