1. 程式人生 > >js監聽css3動畫實現

js監聽css3動畫實現

原理是使用animationstart 監聽動畫的開始,用animationend 監聽動畫的結束,用animationiteration監聽動畫的重複播放,用transitionend監聽過渡完成,下面是原始碼:

<!DOCTYPE html>
<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>Document</title> <style type="text/css"> .main { width: 100px; height: 100px; border: 1px solid #333; } .change-size { transition: all 1s; transform: translateX(100px); animation: changeBig 2
s 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>