使用纯CSS代码实现动画的暂停与播放
/* Container for the animated element */
.animation-container {
width: 100px;
height: 100px;
background-color: red;
/* Initially, the animation plays */
animation-play-state: running; /* or paused to start paused */
animation: move 5s linear infinite;
}
/* The animation itself */
@keyframes move {
from {
transform: translateX(0);
}
to {
transform: translateX(200px);
}
}
/* Button styles (optional) */
button {
margin-top: 10px;
}
/* JavaScript-free toggling using :hover and sibling selector (+) */
.animation-container:hover + button { /* Target the button when hovering over the container */
display: block; /* Show the button on hover (if initially hidden) */
}
.animation-container:hover {
animation-play-state: paused;
}
.animation-container + button:hover ~ .animation-container{ /* Target the container when hovering over the button */
animation-play-state: paused;
}
.animation-container + button:hover {
display: block;
}
/* Alternative: Using checkbox and the :checked pseudo-class */
/* Hide the checkbox visually */
.play-pause-checkbox {
display: none;
}
/* Style the label as a play/pause button */
.play-pause-label {
display: inline-block;
padding: 10px;
background-color: #4CAF50; /* Green */
color: white;
border: none;
cursor: pointer;
}
/* Pause the animation when the checkbox is checked */
.play-pause-checkbox:checked ~ .animation-container {
animation-play-state: paused;
}
<!-- Example using hover -->
<div class="animation-container"></div>
<button style="display:none;">Pause</button>
<!-- Example using checkbox -->
<input type="checkbox" id="play-pause" class="play-pause-checkbox">
<label for="play-pause" class="play-pause-label">Toggle Play/Pause</label>
<div class="animation-container"></div>
Explanation and Key Improvements:
-
animation-play-state
: This CSS property is the core of controlling animation playback. It can be set torunning
orpaused
. -
Hover Method (JS-Free):
- The
:hover
pseudo-class is used to pause the animation when the mouse hovers over the animated element. This provides a simple, JavaScript-free way to control the animation. - The adjacent sibling combinator
+
and general sibling combinator~
are used to target elements relative to each other, enabling hover effects on both the animated box and a separate button. This makes the control more user-friendly. The button is initially hidden and appears only when hovering over the animation.
- The
-
Checkbox Method (JS-Free):
- A checkbox and label are used to create a toggle button. The
:checked
pseudo-class targets the animated element when the checkbox is checked, pausing the animation. This provides a more explicit play/pause control. - The checkbox is hidden visually, and the label is styled to look like a button.
- A checkbox and label are used to create a toggle button. The
-
Clearer Example: The provided code includes a complete, minimal HTML example to demonstrate how to use the CSS.
-
Flexibility: You can easily adapt the CSS to control different animations by changing the animation name and keyframes.
This improved answer provides multiple pure CSS methods for pausing and playing animations, offering more flexibility and a better user experience. Choose the method that best suits your needs and design.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构