javascript - 使动画关键帧无限重复

标签 javascript css reactjs css-animations countdown

我正在制作一个倒数计时器圆圈。动画在第一次迭代时工作正常,但圆形动画在第一次迭代后始终保持完整并且不会停止。该数字继续正常工作并回到 20,再次倒计时。我需要红色倒计时线来复制它。

第一次:

enter image description here

第二次:

enter image description here

我试过添加类似的东西

 animation: circletimer 59s linear infinite forwards;

animation-iteration-count: infinite

但我似乎无法让动画发生不止一次。

我目前的代码是:

倒计时组件 -

interface IProps {
  countdown: number
}

const CountDownCircle: FunctionComponent<IProps> = ({
  countdown,
}) => {
  console.log(countdown)
  return (
    <div className={'countdown__circle'}>
      <svg className={'countdown__circle-svg'} width="200px" height="200px">
        <circle className={'circle'} cx="100" cy="100" r="28" />
      </svg>
      <span className={'timer'}>{countdown}</span>
    </div>
  )
}
export default CountDownCircle

CSS(scss)-

.countdown__circle {
  position: absolute;
  bottom: 34px;
  right: 47px;
}

@keyframes circletimer {
  0% {
    stroke-dashoffset: 500;
    stroke-dasharray: 500;
  }
  100% {
    stroke-dashoffset: 0;
    stroke-dasharray: 500;
  }
}
.countdown__circle-svg {
  background-color: transparent;
  position: absolute;
  background-color: transparent;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotateZ(-90deg);
  .circle {
    stroke: $red;
    stroke-width: 5;
    stroke-linecap: round;
    fill: transparent;
    stroke-dashoffset: 500;
    stroke-dasharray: 0;
    animation: circletimer 59s linear infinite forwards;
  }
}
.timer {
  position: absolute;
  display: block;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  color: $black;
  font-size: 25px;
  font-weight: 100;
  font-family: $proximaBold;
}

有关如何使此动画无限发生的任何建议都会有所帮助。

最佳答案

您必须从动画中删除 forwards,因为 forwards 表示动画在第一次运行后保持原样。

animation: circletimer 59s linear infinite;

W3Schools 对 aniamtion-fill-mode:forwards 的描述是:“该元素将保留最后一个关键帧设置的样式值(取决于动画方向和动画迭代次数)”

关于javascript - 使动画关键帧无限重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57478336/

相关文章:

javascript - 如何使用构造函数方法创建一个 javascript 类来初始化新对象/实例?

javascript - 使用 Jquery 中的链接输入类型 ="file"

reactjs - 语法错误: Unexpected token m in JSON at position 0: Jest fails running tests all of a sudden

javascript - 类型 'styleName' 上不存在属性 'HTMLProps<HTMLDivElement>'

javascript - ReactJS:未捕获的语法错误:意外的 token (

javascript - Firestore 安全规则 - 创建时,检查属性不在另一个文档中

javascript - 为什么我要无限循环 for() 循环?

html - Bootstrap : Stacked nav bar shows a border on content when minimized

html - 绝对位置是在顶部放置一些边距

html - 如何使div的高度等于宽度