css3动画如何解决动画的播放、暂停和重新开始
2024-10-07 23:00:12
0921自我总结
css3如何解决动画的播放、暂停和重新开始
一.解决的本质思路
播放的解决思路
先定义好动画效果通过类名的增加达到样式的出现
暂停的解决思路
我们播放动画时,如要暂停动画,就要用到animation-play-state
这个属性。animation-play-state
属性有两个值:
paused: 暂停动画;
running: 继续播放动画;
当然去掉animation-play-state
,也可以继续播放动画。
重新开始解决思路
播放与重新开始的解决办法
对于元素取多个类名,通过类名的删除,替换
注意点:这里不能删除和添加类名为同一个
,而且动画要同一效果,不同动画名称
.不然动画效果无法重置
二.演示代码
<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>
<style>
@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;
}
</style>
<script>
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;
});
</script>
最新文章
- EBS安装提示libXtst.so.6: cannot open shared object file
- CoreLocation
- OC基础(3)
- Android下将图片载入到内存中
- QUI操作超时弹出登录窗口登录的处理方式
- java学习面向对象之匿名内部类
- Android_CodeWiki_03
- ShoneSharp语言(S#)的设计和使用介绍—数值Double
- PHP中静态方法和实例化方法的区别
- 关于 RESTful API 中 HTTP 状态码的定义
- css样式之input输入框默认样式
- 10.scrapy框架简介和基础应用
- Codeforces 1139D Steps to One dp
- ESP NVS
- haar特征(转)
- Linux command stty
- 利用反射--调用一个按钮的Click事件
- 【转】Java学习---Java的锁和Mysql的锁机制
- util.promisify 的那些事儿
- POJ-2018(二分)
热门文章
- Educational Codeforces Round 78 (Rated for Div. 2)
- 计算机等级考试真题1(JAVA)
- HttpRunner学习8--使用debugtalk.py辅助函数
- SpringCloud-使用路由网关统一访问接口(附代码下载)
- CSS样式 解决文字过长显示省略号问题
- JS---BOM---定时器
- Win32 API编程——前言
- Pick of the Week&#39;19 | 图数据库 Nebula 第 47 周看点-- insert 的二三事
- 3.java基础之关键字instanceof
- Cesium专栏-淹没分析(附源码下载)