大网数据广告活动
Web 标准与前端开发

animation-play-state

· 52CE 我爱测

**`animation-play-state`** CSS 属性设置动画是运行还是暂停。

animation-play-state CSS 属性设置动画是运行还是暂停。

CSS Demo: animation-play-state

animation-play-state: paused;
animation-play-state: running;
<section class="flex-column" id="default-example">
  <div class="animating" id="example-element"></div>
</section>
#example-element {
  background-color: #1766aa;
  color: white;
  margin: auto;
  margin-left: 0;
  border: 5px solid #333;
  width: 150px;
  height: 150px;
  border-radius: 50%;
}

.animating {
  animation-name: slide;
  animation-duration: 3s;
  animation-timing-function: ease-in;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

@keyframes slide {
  from {
    background-color: orange;
    color: black;
    margin-left: 0;
  }
  to {
    background-color: orange;
    color: black;
    margin-left: 80%;
  }
}

恢复暂停的动画将从暂停时停止的位置开始播放,而不是从动画序列的开头重新开始播放。

语法

/* 单个动画 */
animation-play-state: running;
animation-play-state: paused;

/* 多个动画 */
animation-play-state: paused, running, running;

/* 全局值 */
animation-play-state: inherit;
animation-play-state: initial;
animation-play-state: revert;
animation-play-state: revert-layer;
animation-play-state: unset;

值

  • running
    • : 当前动画正在运行。
  • paused
    • : 当前动画已被停止。

[!NOTE] 当你在 animation-* 属性上指定多个逗号分隔的值时,它们将按照 animation-name 出现的顺序应用于动画。对于动画数量和 animation-* 属性值不匹配的情况,请参见设置多个动画属性值。

形式定义

形式语法

示例

暂停动画

这个动画被暂停了,但是当你将鼠标悬停在上面时会继续运行。

HTML

<div class="box"></div>
.box {
  background-color: rebeccapurple;
  border-radius: 10px;
  width: 100px;
  height: 100px;
  animation-name: rotate;
  animation-duration: 0.7s;
  animation-iteration-count: infinite;
  animation-play-state: paused;
}

.box:hover {
  animation-play-state: running;
}

@keyframes rotate {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}

结果

将鼠标悬停在矩形上来播放动画。

暂停动画

参见 CSS 动画以获取示例。

规范

浏览器兼容性

参见

  • 使用 CSS 动画
  • JavaScript AnimationEvent API
  • 其他相关的动画属性:animation、animation-composition、animation-delay、animation-direction、animation-duration、animation-fill-mode、animation-iteration-count、animation-name、animation-timeline、animation-timing-function

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。