【问题标题】:How to have 3 @keyframes fill's on the same page?如何在同一页面上填充 3 个 @keyframes?
【发布时间】:2021-08-24 00:09:24
【问题描述】:

我正在尝试让三个关键帧彼此独立工作

我在朋友的 codepen 帐户上添加了一些 HTML 和 CSS 给你看。

https://codepen.io/williamharvey/pen/JjJjRdz

我有三个圆形表盘,它们具有以下功能。

.circle-wrap .circle .fill {
  animation: fill ease-in-out 3s;
  transform: rotate(45deg);
}

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

但我希望第二个刻度盘是 65 度

.circle-wrap .circle .fill {
  animation: fill ease-in-out 3s;
  transform: rotate(65deg);
}

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

还有第三个95度

.circle-wrap .circle .fill {
  animation: fill ease-in-out 3s;
  transform: rotate(95deg);
}

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

这可能吗?

提前致谢。

【问题讨论】:

  • 寻求代码帮助的问题必须包括在问题本身中重现它所需的最短代码,最好是在堆栈片段中。尽管您提供了一个链接,但如果它变得无效,那么您的问题对于未来遇到同样问题的其他 SO 用户将毫无价值。见Something in my website/example doesn't work can I just paste a link
  • 您需要为正在动画的表盘添加更多特异性。

标签: css css-animations keyframe


【解决方案1】:

简短的回答:是的。

您可以使用 forwards(关键字)将动画冻结在其结束值上,并使用 CSS var() 应用来自不同元素但来自单个规则的特定值:

您的代码示例:

.circle-wrap {
  margin: 50px auto;
  width: 240px;
  height: 240px;
  background: #e5e5e5;
  border-radius: 50%;
  transform: rotate(-125deg);
}

.circle-wrap .circle .mask,
.circle-wrap .circle .fill {
  width: 240px;
  height: 240px;
  position: absolute;
  border-radius: 50%;
}

.circle-wrap .circle .mask {
  clip: rect(0px, 240px, 240px, 120px);
}

.circle-wrap .circle .mask .fill {
  clip: rect(0px, 120px, 240px, 0px);
  background-color: #ffe100;
}

.circle-wrap .circle .mask.full,
.circle-wrap .circle .fill {
  animation: fill ease-in-out 3s forwards;
}

@keyframes fill {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate( var(--rt));
  }
}

.circle-wrap .inside-circle {
  width: 185px;
  height: 185px;
  border-radius: 50%;
  background: #fff;
  line-height: 185px;
  text-align: center;
  top: 28px;
  left: 28px;
  z-index: 100;
  font-weight: 700;
  font-size: 6.5rem;
  letter-spacing: -4px;
  transform: rotate(114deg);
  font-style: italic;
  font-family: brandon-grotesque;
  padding: 0;
  position: relative;
}

.circle-wrap .inside-circle span {
  font-size: 2.5rem;
  letter-spacing: 0;
}

.circle-wrap .inside-circle .cone {
  width: 0;
  height: 0;
  border-left: 175px solid transparent;
  border-right: 175px solid transparent;
  border-top: 125px solid white;
  border-radius: 50%;
  transform: rotate(192deg);
  position: absolute;
  bottom: -33px;
  left: -96px;
  z-index: -1;
}

.circle-wrap .inside-circle .cone .dial-speeds {
  transform: rotate(179deg);
  position: relative;
  z-index: 1;
  font-weight: 400;
  font-style: normal;
}

.circle-wrap .inside-circle .cone .dial-speeds .left {
  position: absolute;
  bottom: -78px;
  width: 18px;
  height: 20px;
  left: -50px;
}

.circle-wrap .inside-circle .cone .dial-speeds .right {
  position: absolute;
  bottom: -78px;
  width: 18px;
  height: 20px;
  right: -50px;
}

.circle-wrap .inside-circle .cone .dial-speeds .right span {
  right: -16px;
  top: -58px;
  position: absolute;
  font-size: 15px;
}

.circle-wrap .inside-circle .cone .dial-speeds .left span {
  left: -16px;
  top: -58px;
  position: absolute;
  font-size: 15px;
}
<div class="grid gap-4 grid-cols-3 text-left pt-24">
  <div class="circle-wrap">
    <div class="circle">
      <div class="mask full">
        <div class="fill fill"></div>
      </div>
      <div class="mask half">
        <div class="fill fill" style="--rt:45deg"></div>
      </div>
      <div class="inside-circle">
        300<span></span>
        <div class="cone">
          <div class="dial-speeds">
            <div class="left">
              <img src="">
              <span>300</span>
            </div>
            <div class="right">
              <img src="">
              <span>100</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="circle-wrap">
    <div class="circle">
      <div class="mask full">
        <div class="fill"></div>
      </div>
      <div class="mask half">
        <div class="fill" style="--rt:60deg"></div>
      </div>
      <div class="inside-circle">
        500<span></span>
        <div class="cone">
          <div class="dial-speeds">
            <div class="left">
              <img src="">
              <span>500</span>
            </div>
            <div class="right">
              <img src="">
              <span>200</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="circle-wrap">
    <div class="circle">
      <div class="mask full">
        <div class="fill" style="--rt:95deg"></div>
      </div>
      <div class="mask half">
        <div class="fill"></div>
      </div>
      <div class="inside-circle">
        900<span></span>
        <div class="cone">
          <div class="dial-speeds">
            <div class="left">
              <img src="">
              <span>900</span>
            </div>
            <div class="right">
              <img src="">
              <span>300</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

资源:

【讨论】:

  • 非常感谢@G-Cyrillus!这一切都很完美,但我也从你的回答中学到了很多东西。谢谢!
  • 我似乎很难让黄色进度条超过 180 度标记。你能指出我正确的方向吗?我希望将黄色平滑旋转到 270 度。 @G-Cyrillus
  • @JohnKBell 你有我可以看到和使用的钢笔或 jsfiddle 吗?
  • 好的,现在开始设置。
  • 这是我的第一个代码块的小提琴 - jsfiddle.net/williamharvey/210Lwbt5 @G-Cyrillus
猜你喜欢
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
  • 2020-09-27
  • 2012-03-26
相关资源
最近更新 更多