js监听css3动画实现

原理是使用animationstart 监听动画的开始,用animationend 监听动画的结束,用animationiteration监听动画的重复播放,用transitionend监听过渡完成,下面是源码:


<html lang="zh">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Documenttitle>
  <style type="text/css">
    .main {
      width: 100px;
      height: 100px;
      border: 1px solid #333;
    }

    .change-size {
      transition: all 1s;
      transform: translateX(100px);
      animation: changeBig 2s linear 1s 3 normal;
    }

    @keyframes changeBig {
      to {
        width: 200px;
        height: 200px;
      }
    }
  style>
head>

<body>
  <div class="main">点击我div>
  <script src="//cdn.bootcss.com/zepto/1.2.0/zepto.min.js">script>
  <script type="text/javascript">
    var $main = $('.main');

    $main.on('click', function() {
      $(this).addClass('change-size')
    });
    $main.on('animationstart', function(e) {
      console.log('animationstart:' + e.elapsedTime)
    });
    $main.on('animationiteration', function(e) {
      console.log('animationiteration:' + e.elapsedTime)
    });
    $main.on('animationend', function(e) {
      console.log('animationend:' + e.elapsedTime)
    });
    $main.on('transitionend', function(e) {
      console.log('transitionend:' + e.elapsedTime)
    });
  script>
body>
html>

你可能感兴趣的:(js监听css3动画实现)