css3一直旋转 暂停后还保持角度

要使CSS3动画一直旋转,并在暂停时保持最后的角度,您可以使用animation-play-state: paused;属性来暂停动画,并使用animation-fill-mode: forwards;来保留最后的状态。

CSS代码:

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.rotating-element {
  animation: rotate 2s linear infinite;
  animation-play-state: running; /* 默认运行动画 */
  animation-fill-mode: forwards; /* 在动画结束时保留最后的状态 */
}

.rotating-element.paused {
  animation-play-state: paused; /* 添加一个类来暂停动画 */
}

HTML代码:

<div class="rotating-element">旋转我</div>
<button onclick="toggleRotation()">暂停/继续</button>

<script>
function toggleRotation() {
  var element = document.querySelector('.rotating-element');
  element.classList.toggle('paused');
}
</script>

在这个例子中,一个元素被赋予了一个无限循环的旋转动画。当点击按钮时,会通过JavaScript切换.paused类,暂停或继续动画。由于animation-fill-mode设置为forwards,动画暂停时会保留在最后的角度。

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值