如果用 animation: none 实现动画暂停会导致恢复时从动画序列的开头重新开始播放。
animation-play-state 属性设置动画是运行还是暂停。恢复暂停的动画将从暂停时停止的位置开始播放,而不是从动画序列的开头重新开始播放。
看看效果:
鼠标移上去暂停,移走恢复。
完整代码如下:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>animation-play-state</title>
</head>
<style>
.box {
width: 50vw;
margin: 40vh auto;
overflow: hidden;
padding: 1vh 0;
background: #333;
color: red;
font-size: 10vh;
}
.msg {
display: inline-block;
white-space: nowrap;
padding-left: 100%;
will-change: transform;
animation: lamp 10s linear infinite;
}
.box:hover .msg {
animation-play-state: paused;
}
@keyframes lamp {
0% {
transform: translateX(0);
}
100% {
transform: translateX(-100%);
}
}
</style>
<body>
<div class="box">
<div class="msg">animation-play-state 属性设置动画是运行还是暂停。恢复暂停的动画将从暂停时停止的位置开始播放,而不是从动画序列的开头重新开始播放。</div>
</div>
</body>
</html>