You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CSS实现的时钟、弹跳球两个动画无法同时运行如何解决

问题根因

两个CSS语法错误导致样式解析异常:

  • @keyframes rotation 规则缺少闭合大括号。CSS解析遇到未闭合的代码块时,会将后续所有样式规则(包括.box、.ball样式和@keyframes bounce动画规则)全部识别为rotation关键帧的内部内容,导致弹跳球的样式和动画规则完全失效,因此出现消失的问题。删除@keyframes rotation规则后,后续样式不再被错误嵌套,弹跳球自然恢复显示。
  • @keyframes bounce 存在两处问题:一是from关键帧存在笔误,将100px 0px错写为100pxz 0px,属于无效属性值,会导致动画解析异常;二是整个bounce关键帧本身也缺少闭合大括号,会进一步加剧解析错误。
修复后完整代码

CSS部分

.clock {
  position: relative;
  width: 400px;
  height: 400px;
  border: 10px solid;
  border-radius: 50%;
}

#second {
  position: absolute;
  background: black;
  width: 2px;
  height: 180px;
  margin: 20px 199px;
  animation: rotation 60s infinite linear;
  transform-origin: bottom left;
}

#minute {
  position: absolute;
  background: black;
  width: 6px;
  height: 140px;
  margin: 60px 197px;
  animation: rotation 3600s infinite linear;
  transform-origin: bottom right;
}

#hour {
  position: absolute;
  background: black;
  width: 10px;
  height: 120px;
  margin: 80px 195px;
  animation: rotation 43200s infinite linear;
  transform-origin: bottom left;
}

@keyframes rotation {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(359deg);
  }
} /* 补全rotation缺失的闭合大括号 */

.box {
  position: relative;
}
.ball {
  position: absolute;
  height: 100px;
  width: 100px;
  border: 3px solid pink;
  border-radius: 50%;
  animation: bounce 2s infinite;
  background: pink;
}

.step1 {
  position: absolute;
  top: 150px;
  height: 10px;
  width: 220px;
  background: cyan;
}

.step2 {
  position: absolute;
  top: 150px;
  left: 220px;
  margin-top: 20px;
  height: 10px;
  width: 220px;
  background: cyan;  
}

.step3 {
  margin-top: 40px;
  position: absolute;
  left: 440px;
  top: 150px;
  height: 10px;
  width: 220px;
  background: cyan;
}

@keyframes bounce {
  from {margin: 100px 0px;} /* 修正笔误,删除多余的z */
  to{margin: 50px 500px;} 
  0% {
    transform: translateY(-30%);
  }
  12% {
    transform: translateY(33%);
  }
  24% {
    transform: translateY(-66%);
  }
  36% {
    transform: translateY(33%);
  }
  48% {
    transform: translateY(-20%);
  }
  100% {
    transform: translateY(33%);
  }
} /* 补全bounce缺失的闭合大括号 */

HTML部分

<div class="clock">
  <div id="hour"></div>
  <div id="minute"></div>
  <div id="second"></div>
</div>

<div class="box">
  <div class="step1"></div>
  <div class="step2"></div>
  <div class="step3"></div>
  <div class="ball"></div>
</div>

替换为上述代码后,时钟转动、弹跳球两个动画即可同时正常运行。

内容的提问来源于stack exchange,提问作者Marius Uzemeckas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 13:06:24