css3 动画的播放、暂停和重新开始
2017-11-03 17:33
267 查看
播放
先在@keyframes中创建动画,之后把它捆绑到某个选择器,就可以产生动画效果。
html
<div id="box" class="box"></div>
css
@keyframes mymove { 0% { margin-left: 0px; } 50% { margin-left: 400px; } 100% { margin-left: 0px; } } .box { margin: 50px 0; width: 100px; height: 100px; background-color: #5578a2; animation: mymove 5s infinite ease; }
暂停
我们播放动画时,如要暂停动画,就要用到animation-play-state这个属性。
animation-play-state属性有两个值:
paused: 暂停动画; running: 继续播放动画;
当然去掉
animation-play-state,也可以继续播放动画。
重新开始
如果要重新开始动画,加载一个相同的动画,不同名字,可以达到重新开始动画的效果。效果:
代码部分:
html
<div id="box" class="box"></div> <p id="text"></p> <div class="control"> <button id="play" value="播放">播放</button> <button id="pause" value="暂停">暂停</button> <button id="restart" value="重新开始">重新开始</button> </div>
css
@keyframes mymove { 0% { margin-left: 0px; } 50% { margin-left: 400px; } 100% { margin-left: 0px; } } @keyframes mymove1 { 0% { margin-left: 0px; } 50% { margin-left: 400px; } 100% { margin-left: 0px; } } .box { margin: 50px 0; width: 100px; height: 100px; background-color: #5578a2; } .play { animation: mymove 5s infinite ease; } .restart { animation: mymove1 5s infinite ease; } .pause { animation-play-state: paused; }
js
var play = document.getElementById('play'), pause = document.getElementById('pause'), restart = document.getElementById('restart'), text = document.getElementById('text'), box = document.getElementById('box'); pause.addEventListener('click', function() { if (box.classList.contains('play')) { box.className = 'pause play box'; } else { box.className = 'pause restart box'; } text.innerHTML = this.value; }); play.addEventListener('click', function() { if (box.classList.contains('play')) { box.className = 'play box'; } else { box.className = 'restart box'; } text.innerHTML = this.value; }); restart.addEventListener('click', function() { if (box.classList.contains('play')) { box.className = 'restart box'; } else { box.className = 'play box'; } text.innerHTML = this.value; });
相关文章推荐
- 暂停和播放CSS3动画的两种实现方法
- css3 事件触发动画 可重复播放
- 如何实现平移动画的暂停与播放?
- 难道是timescale惹的祸?(播放暂停动画为何不出现)
- 关于Unity中如何立即中断动画然后重新开始播放
- gifplayer-可控制GIF动画图片播放和暂停jQuery插件
- [Unity基础]游戏暂停下播放角色与粒子动画
- Acticle 15:css3动画小例子(幻灯片播放)
- css3动画应用-音乐唱片旋转播放特效
- 谈谈一些有趣的CSS题目(十四)-- 纯 CSS 方式实现 CSS 动画的暂停与播放!
- 纯 CSS 方式实现 CSS 动画的暂停与播放!
- 动画的暂停与继续播放
- CSS3两个动画顺序衔接播放
- 【CSS3】设置动画播放方向
- 实现动画暂停播放
- unity3d新版动画系统模型动画播放,暂停,倒退播放
- 【CSS3】设置动画的播放状态
- SASS使用CSS3动画并使动画暂停和停止在最后一帧的简单例子
- 纯 CSS 方式实现 CSS 动画的暂停与播放
- [ActionScript 3.0] 如何控制加载swf动画的播放与暂停