【问题标题】:CSS animation automatically goes to defaultCSS动画自动进入默认值
【发布时间】:2021-09-08 10:16:26
【问题描述】:

https://jsfiddle.net/t3w1Lqr0/1/
当我运行此代码时,它有时会按预期工作,有时会出现故障并转到0deg
请看下面的 gif ↓
我将它用于electron
我大部分时间在jsfiddle工作,但很少在电子工作。
Electron v13.1.2
Chromium v91.0.4472.77
Node v14.16.0

【问题讨论】:

    标签: javascript html electron chromium


    【解决方案1】:

    您可以完全在 CSS 中创建动画并使用 animation-fill-mode: forwards; 来保留动画的最后一帧。

    * {
      box-sizing: border-box; margin: 0; padding: 0;
    }
    html, body { height: 100%; }
    body {
      display: flex; justify-content: center; align-items: center;
      background-color: #eee;
    }
    div, hr::before {
      overflow: hidden; position: absolute;
      width: 10rem; height: 5rem;
    }
    hr {
      border-style: solid; border-width: 1.5rem;
      border-color: #333; border-radius: 10rem;
      width: 10rem; height: 10rem;
    }
    hr::before {
      content: '';
      transform-origin: 50% 100%; transform: translateX( -50% );
      bottom: 0.25rem; left: 50%;
      border-radius: 100% 100% 0.5rem 0.5rem;
      width: 0.5rem; height: 4.5rem;
      background-color: #ccc;
      animation-name: rotate; animation-duration: 2s;
      animation-timing-function: ease-in-out;
      animation-fill-mode: forwards; /* Force last frame of animation to stay */
    }
    @keyframes rotate {
      0% { transform: translateX( -50% ) rotate( -90deg ); }
      100% { transform: translateX( -50% ) rotate( 90deg ); }
    }
    <div> <hr> </div>

    如果您想在动画像 JavaScript 那样开始播放之前添加延迟,您可以使用 CSS 属性 animation-delay

    【讨论】:

    • js 在那里,以便我可以动态控制仪表何时停止!使用纯CSS我无法控制!我如何在 # 击键百分比处停止仪表rotate
    • 我刚刚添加了那行,它完美运行?
    • 我可以编辑你的帖子并添加我的新代码吗!
    • 我刚刚添加了那行并将animation-timing-function: ease-in-out; 更改为animation-timing-function: linear;,它完美运行?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    相关资源
    最近更新 更多