使用纯 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中文网其他相关文章!

Wufoo一直在集成方面非常出色。他们与特定应用程序(例如广告系列显示器,MailChimp和Typekit)进行集成,但他们也


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器