使用纯 css 的方法,暂停或播放 css 动画。是不是看起来应该是不可能的实现的;或者就算可以实现,也是一个很麻烦的实现方法,需要用大量的css样式才可以实现。其实不然,在 css3 animation 中,就有这样一个属性可以做到暂停、播放动画。本章就给大家介绍如何用纯css方式实现css动画的暂停与播放效果?animation-play-state属性介绍(详解)。有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
animation-play-state属性
animation-play-state: paused | running;
animation-play-state: 属性定义一个动画是否运行或者暂停。可以通过查询它来确定动画是否正在运行。另外,它的值可以被设置为暂停和恢复的动画的重放。
如果借助 Javascript,我们可以实现控制 CSS 动画的运行和播放,下面列出部分关键代码:
html代码:
立即学习“前端免费学习笔记(深入)”;
<div class="btn">stop</div> <div class="animation"></div>
css代码:
.animation {
width: 100px;
height: 100px;
margin: 50px auto;
background: deeppink;
animation: move 2s linear infinite alternate;
}
@keyframes move {
0% {
transform: translate(-100px, 0);
}
100% {
transform: translate(100px, 0);
}
}
.btn {
width: 50px;
margin: 10px auto;
text-align: center;
border:1px solid #ddd;
padding: 10px;
border-radius: 5px;
cursor:pointer;
&:hover {
background: #ddd;
color: #333;
}
&:active {
background: #aaa;
}
}js代码:
document.querySelector('.btn').addEventListener('click', function() {
let btn = document.querySelector('.btn');
let elem = document.querySelector('.animation');
let state = elem.style['animationPlayState'];
if(state === 'paused') {
elem.style['animationPlayState'] = 'running';
btn.innerText = 'stop';
} else {
elem.style['animationPlayState'] = 'paused';
btn.innerText = 'play';
}
});效果图(播放时和停止播放后):


纯 CSS 实现
下面我们探讨下,使用纯 CSS 的方式能否实现。
hover 伪类实现
使用 hover 伪类,在鼠标悬停在按钮上面时,控制动画样式的暂停。
关键代码如下:
html代码:
立即学习“前端免费学习笔记(深入)”;
<div class="btn stop">stop</div> <div class="animation"></div>
css代码:
.animation {
width: 100px;
height: 100px;
margin: 50px auto;
background: deeppink;
animation: move 2s linear infinite alternate;
}
input {
display: none;
}
@keyframes move {
0% {
transform: translate(-100px, 0);
}
100% {
transform: translate(100px, 0);
}
}
.btn {
width: 50px;
margin: 10px auto;
text-align: center;
border:1px solid #ddd;
padding: 10px;
border-radius: 5px;
cursor:pointer;
&:hover {
background: #ddd;
color: #333;
}
&:active {
background: #aaa;
}
}
.stop:hover ~ .animation {
animation-play-state: paused;
}效果图:


当然,这个方法不够智能,如果释放鼠标的自由,点击一下暂停、再点击一下播放就好了。还有其他方法吗?
checked 伪类实现
之前的文章《有趣的 CSS 题目(8):纯CSS的导航栏Tab切换方案》也谈过,使用 radio 标签的 checked 伪类,加上 实现纯 CSS 捕获点击事情。
并且利用被点击的元素可以控制一些 CSS 样式。实现如下:
html代码:
<input id="stop" type="radio" name="playAnimation"/>
<input id="play" type="radio" name="playAnimation"/>
<div class="box">
<label for="stop">
<div class="btn">stop</div>
</label>
<label for="play">
<div class="btn">play</div>
</label>
</div>
<div class="animation"></div>css代码:
.animation {
width: 100px;
height: 100px;
margin: 50px auto;
background: deeppink;
animation: move 2s linear infinite alternate;
}
input {
display: none;
}
@keyframes move {
0% {
transform: translate(-100px, 0);
}
100% {
transform: translate(100px, 0);
}
}
.btn {
width: 50px;
margin: 10px auto;
text-align: center;
border:1px solid #ddd;
padding: 10px;
border-radius: 5px;
cursor:pointer;
&:hover {
background: #ddd;
color: #333;
}
&:active {
background: #aaa;
}
}
#stop:checked ~ .animation {
animation-play-state: paused;
}
#play:checked ~ .animation {
animation-play-state: running;
}我们希望当 #stop 和 #play 两个 radio 被点击时,给 .animation 元素分别赋予 animation-play-state: paused 或是 animation-play-state: running 。而且二者只能生效其一,所以需要给两个 radio 标签赋予相同的 name 属性。
效果图:


上面的示例中,实现了纯 CSS 方式实现 CSS 动画的暂停与播放。
当然,还有一些其他方法,例如 radio 替换成 checkbox ,或者使用 :target 伪类选择器也能实现上面同样的效果,感兴趣的可以尝试一下。
以上就是如何用纯CSS方式实现CSS动画的暂停与播放效果?animation-play-state属性介绍(详解)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号