【问题标题】:How can I get working keyframe animation?如何获得有效的关键帧动画?
【发布时间】:2020-09-09 01:10:19
【问题描述】:

我正在尝试将动画制作成涟漪效果。它似乎在 chrome 浏览器上运行良好,但在 safari 上运行不正常,而且我在同一页面中有其他动画在 chrome 和 safari 上运行良好,但不是这个。我想知道我做错了什么。

我尝试调试它,我可以看到 Safari 图形选项卡中有一条消息说

“这个动画没有关键帧”

我的css代码:

.ripple-animation {
    &::after {
        @include rings(2s, 40s);
    }
    &::before {
        @include rings(2s, 40s);
    }
}

@mixin rings($duration, $delay) {
    opacity: 0.5;
    // display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: -8px;
    left: -10px;
    right: 0;
    bottom: 0;
    content: '';
    height: 120%;
    width: 110%;
    border: 4px solid rgba(0, 0, 0, 0.4);
    border-radius: 100%;

    -webkit-animation-name: ripple;
    -webkit-animation-duration: $duration;
    -webkit-animation-delay: $delay;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-timing-function: cubic-bezier(.65, 0, .34, 1);

    animation-name: ripple;
    animation-duration: $duration;
    animation-delay: $delay;
    animation-iteration-count: infinite;
    animation-timing-function: cubic-bezier(.65, 0, .34, 1);

}

@-webkit-keyframes ripple {
    from {
        opacity: 1;
        -webkit-transform: scale3d(0.8, 0.8, 0.5);
        transform: scale3d(0.8, 0.8, 0.5);
    }
    to {
        opacity: 0;
        -webkit-transform: scale3d(1.1, 1.1, 1);
        transform: scale3d(1.1, 1.1, 1);
    }

}

@keyframes ripple {
    from {
        opacity: 1;
        -webkit-transform: scale3d(0.8, 0.8, 0.5);
        transform: scale3d(0.8, 0.8, 0.5);
    }
    to {
        opacity: 0;
        -webkit-transform: scale3d(1.1, 1.1, 1);
        transform: scale3d(1.1, 1.1, 1);
    }
}

【问题讨论】:

  • 请包含完整代码,使其至少可运行
  • 不要在@keyframes 中使用-webkit-transform,只能在@-webkit-keyframes 中使用
  • 你的 safari 版本是什么?

标签: html css safari css-animations keyframe


【解决方案1】:
  1. Autoprefixer 是一个 VS Code 扩展,通过添加像 -webkit-animation-name: 这样的额外 css 来确保你的 css 在任何地方都兼容,以使你的代码兼容

  2. (对于使用 SASS 的人)在 VS Code 中使用 SASS 编译器(由 ritwickey 提供)。它会自动生成 css,确保你的 css 在任何地方都兼容

【讨论】:

    【解决方案2】:

    原因可能是您的浏览器不支持该代码中使用的内容。那个“东西”实际上是动画和过渡中的伪元素::before::after

    即使使用-webkit,也没有 Safari 浏览器支持此功能。 Safari iOS 也是如此。


    跨浏览器支持 ::before::after 以及动画和过渡。

    【讨论】:

      【解决方案3】:

      可惜我没有 Mac 来检查/调试你的代码,你可以试试0% - 100% 而不是from - to? Safari 有时会有一些奇怪的怪癖。

      例如:

       @keyframes ripple {
           0% {
               opacity: 1;
               transform: scale3d(0.8, 0.8, 0.5);
           }
      
      
           100% {
               opacity: 0;
               transform: scale3d(1.1, 1.1, 1);
      
           }
       }
      

      【讨论】:

        【解决方案4】:

        在 iOS 中,这似乎与在辅助功能设置中关闭了减少运动有关。你必须取消它,而且我认为如果仍然不能正常工作,你必须更改 safari 的版本

        【讨论】:

          【解决方案5】:

          你用 sass 写的东西。这不是正常的 CSS 语法。我刚刚将您的代码修改为css。这些样式正在应用于 safari。

          如果你想使用 Sass,那么最好使用预编译器将你的 sass 代码编译成 css。

          .ripple-animation {
              background: red;
           }
          
          .ripple-animation::after, .ripple-animation::before {
               opacity: 0.5;
               // display: flex;
               flex-direction: row;
               justify-content: center;
               align-items: center;
               position: absolute;
               top: -8px;
               left: -10px;
               right: 0;
               bottom: 0;
               content: '';
               height: 120%;
               width: 110%;
               border: 4px solid rgba(0, 0, 0, 0.4);
               border-radius: 100%;
          
               -webkit-animation-name: ripple;
               -webkit-animation-duration: 2s;
               -webkit-animation-delay: 1s;
               -webkit-animation-iteration-count: infinite;
               -webkit-animation-timing-function: cubic-bezier(.65, 0, .34, 1);
          
          
               animation-name: ripple;
               animation-duration: 2s;
               animation-delay: 1s;
               animation-iteration-count: infinite;
               animation-timing-function: cubic-bezier(.65, 0, .34, 1);
          }
          
          
          
          
          
          
          
           @-webkit-keyframes ripple {
               from {
                   opacity: 1;
                   -webkit-transform: scale3d(0.8, 0.8, 0.5);
                   transform: scale3d(0.8, 0.8, 0.5);
               }
          
               to {
                   opacity: 0;
                   -webkit-transform: scale3d(1.1, 1.1, 1);
                   transform: scale3d(1.1, 1.1, 1);
          
               }
          
           }
          
           @keyframes ripple {
               from {
                   opacity: 1;
                   -webkit-transform: scale3d(0.8, 0.8, 0.5);
                   transform: scale3d(0.8, 0.8, 0.5);
               }
          
          
               to {
                   opacity: 0;
                   -webkit-transform: scale3d(1.1, 1.1, 1);
                   transform: scale3d(1.1, 1.1, 1);
          
               }
          
           }
          <div class="ripple-animation"></div>

          【讨论】:

          • 我仍然无法在 Safari 中工作。我使用了这个 css 代码。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-22
          • 2019-04-08
          • 2015-09-26
          • 2015-07-12
          • 2019-03-27
          相关资源
          最近更新 更多