gpt4 book ai didi

css - 如何在不返回初始状态的情况下停止css动画?

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:45 25 4
gpt4 key购买 nike

我需要在没有js的情况下在动画中播放暂停。

现在当我按下暂停键时。动画进入初始状态。

如何重置动画,然后从当前状态重新开始?

javascript 的解决方案是存在的。我想要一个关于 css 的解决方案。

#play {
display: none;
}

#play+label[for=play] {
display: inline-block;
color: #fff;
cursor: pointer;
font-size: 13px;
z-index: 99;
padding: 5px;
background: #37A000;
margin-bottom: 15px;
}

#play+label[for=play]>span:nth-of-type(2),
#play:checked+label[for=play]>span:nth-of-type(1) {
display: none;
}

#play:checked+label[for=play] {
background: tomato;
}

#play:checked+label[for=play]>span:nth-of-type(2) {
display: block;
}

.progress {
height: 15px;
background: #555;
position: relative;
}

.progress:after {
width: 0;
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
background: #37A000;
animation-play-state: paused;
}

#play:checked+label[for=play]~.progress:after {
animation: animProgress 7s linear infinite;
}

@keyframes animProgress {
100% {
width: 100%;
}
}
<input type="checkbox" name="play" id="play">
<label for="play">
<span>Play</span>
<span>Pause</span>
</label>

<div class="progress"></div>

最佳答案

将您的动画样式移动到 main :after block 中,然后在选中/选择播放按钮时切换暂停/运行,如下所示:

#play {
display: none;
}

#play+label[for=play] {
display: inline-block;
color: #fff;
cursor: pointer;
font-size: 13px;
z-index: 99;
padding: 5px;
background: #37A000;
margin-bottom: 15px;
}

#play+label[for=play]>span:nth-of-type(2),
#play:checked+label[for=play]>span:nth-of-type(1) {
display: none;
}

#play:checked+label[for=play] {
background: tomato;
}

#play:checked+label[for=play]>span:nth-of-type(2) {
display: block;
}

.progress {
height: 15px;
background: #555;
position: relative;
}

.progress:after {
width: 0;
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
background: #37A000;
animation: animProgress 7s linear infinite;
animation-play-state: paused;
}

#play:checked+label[for=play]~.progress:after {
animation-play-state: running;
}

@keyframes animProgress {
100% {
width: 100%;
}
}
<input type="checkbox" name="play" id="play">
<label for="play">
<span>Play</span>
<span>Pause</span>
</label>

<div class="progress"></div>

关于css - 如何在不返回初始状态的情况下停止css动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48168110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com