暂停和播放CSS3动画的两种实现方法
2016-04-24 19:51
561 查看
1,直接修改animationPlayState
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> div{ background-color: #ff0000; position: absolute; animation:mymove 4s 1; -moz-animation:mymove 4s 1; /* Firefox */ -webkit-animation:mymove 4s 1; /* Safari and Chrome */ -o-animation:mymove 4s 1; /* Opera */ -webkit-animation-fill-mode: forwards ; -animation-fill-mode: forwards ; } @keyframes mymove { from {top: 0} to {top: 100px} } @-webkit-keyframes mymove { from {top: 0} to {top: 100px} } </style> </head> <body> <div id="nice">nice</div> <script> var nice = document.getElementById("nice"); var prefixs = ["","o","moz","webkit"], div = document.createElement("div"), computeStyle, prefixAnimationPlayState; //获取所支持的animationPlayState,IE6,7,8不支持CSS3,就不写currentStyle的兼容代码了 computeStyle = window.getComputedStyle(document.documentElement,""); prefixs.forEach(function(key){ var prefix = !key ? "animationPlayState" : key + "AnimationPlayState"; if(typeof computeStyle[prefix] == "string") prefixAnimationPlayState = prefix; }) setTimeout(function(){ nice.style[prefixAnimationPlayState] = "paused"; },1000); setTimeout(function(){ nice.style[prefixAnimationPlayState] = "running"; },2000); </script> </body> </html>
2,修改class
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> div{ background-color: #ff0000; position: absolute; animation:mymove 4s 1; -moz-animation:mymove 4s 1; /* Firefox */ -webkit-animation:mymove 4s 1; /* Safari and Chrome */ -o-animation:mymove 4s 1; /* Opera */ -webkit-animation-fill-mode: forwards ; -animation-fill-mode: forwards ; } @keyframes mymove { from {top: 0} to {top: 100px} } @-webkit-keyframes mymove { from {top: 0} to {top: 100px} } .paused{ -webkit-animation-play-state: paused!important; -moz-animation-play-state: paused!important;; -o-animation-play-state: paused!important;; -animation-play-state: paused!important;; } .running{ -webkit-animation-play-state: running!important;; -moz-animation-play-state: running!important;; -o-animation-play-state: running!important;; -animation-play-state: running!important;; } </style> </head> <body> <div id="nice">nice</div> <script> var nice = document.getElementById("nice"); vardiv = document.createElement("div");//通过修改class暂停 setTimeout(function(){ nice.className = "paused"; },1000); setTimeout(function(){ nice.className = "running"; },2000); </script> </body> </html>
相关文章推荐
- css3新增的文本高级样式-文本阴影和文本溢出
- [读书笔记] CSS权威指南2: 结构和层叠
- 轮播图的实现
- 购物商城后台页面设计
- 【Qt】一段QPushButton的styleSheet样式分享
- H5+css3+js搭建带验证码的登录页面
- CSS 笔记
- 使用gulp-sass 和 gulp-livereload 自动编译sass文件
- 关于css布局的几篇文章
- css
- 上下DIV固定,中间DIV自适应的HTML+CSS实现
- CSS3D变换/立方体旋转效果
- css
- 【css基础】div父元素根据子元素高度自适应高度
- 【css基础】div父元素根据子元素高度自适应高度
- 动态替换css和js的方法
- css练习
- css
- 《CSS3实战》笔记--弹性盒模型(二)
- qt使用样式表修改控件属性