【问题标题】:SVG Line Animation Reversed in Safari?SVG 线条动画在 Safari 中反转?
【发布时间】:2018-02-22 17:17:43
【问题描述】:

我已经为一个网站构建了非常简单的旋转动画,它们在 Chrome/Firefox 中看起来很棒,但由于某种原因,它们在 Safari 中是反向动画。我已经尝试过改变偏移值,但似乎没有任何效果。有解决办法吗?

.sq {
  width: 50vw;
  height: auto;
  padding: 2.2vw;
}

.path {
  stroke-dasharray: 250;
  stroke-dashoffset: 250;
  animation: line 3s ease forwards;
}

@keyframes line {
  from {
    stroke-dashoffset: -250;
  }
  to {
    stroke-dashoffset: 0;
  }
}
<div class="sq">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 81.32 81.32">
  <defs>
    <style>
      .cls-1{fill:#202020;opacity:0.1;}
      .path{fill:none;stroke:#ef3f44;stroke-miterlimit:10;stroke-width:5px;}
    </style>
  </defs>
  <g id="94" data-name="94">
    <g id="Objects">
      <circle cx="40.66" cy="40.66" r="27.17" class="cls-1"/>
      <path d="M40.66 79.15a38.49 38.49 0 1 1 38.49-38.49 38.53 38.53 0 0 1-38.49 38.49zm0-76.07a37.58 37.58 0 1 0 37.58 37.58A37.63 37.63 0 0 0 40.66 3.08z" class="cls-1"/>
      <path d="M26.83 5.1a38.16 38.16 0 1 0 13.83-2.6" class="path"/>
    </g>
  </g>
</svg> 
</div>

也可以随时在 Codepen 上查看:

https://codepen.io/noahbrennan/pen/RLNWXj

【问题讨论】:

  • 这是一个很酷的效果 - 好吧,这并不好笑 - 也许你使用负破折号偏移是问题

标签: css animation svg safari


【解决方案1】:

为了不使用stroke-dashoffset的负值,可以使用stroke-dashoffset: 500;的正值的两倍

而不是负值

@keyframes line {
  from {
    stroke-dashoffset: -250;
  }
  to {
    stroke-dashoffset: 0;
  }
}

使用双正值:

@keyframes line {
  from {
    stroke-dashoffset: 250;
  }
  to {
    stroke-dashoffset: 500;
  }
}

.sq {
  width: 50vw;
  height: auto;
  padding: 2.2vw;
}

.path {
  stroke-dasharray: 250;
  stroke-dashoffset: 250;
  animation: line 3s ease forwards;
}

@keyframes line {
  from {
    stroke-dashoffset: 250;
  }
  to {
    stroke-dashoffset: 500;
  }
}
<div class="sq">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 81.32 81.32">
  <defs>
    <style>
      .cls-1{fill:#202020;opacity:0.1;}
      .path{fill:none;stroke:#ef3f44;stroke-miterlimit:10;stroke-width:5px;}
    </style>
  </defs>
  <g id="94" data-name="94">
    <g id="Objects">
      <circle cx="40.66" cy="40.66" r="27.17" class="cls-1"/>
      <path d="M40.66 79.15a38.49 38.49 0 1 1 38.49-38.49 38.53 38.53 0 0 1-38.49 38.49zm0-76.07a37.58 37.58 0 1 0 37.58 37.58A37.63 37.63 0 0 0 40.66 3.08z" class="cls-1"/>
      <path d="M26.83 5.1a38.16 38.16 0 1 0 13.83-2.6" class="path"/>
    </g>
  </g>
</svg> 
</div>

【讨论】:

    【解决方案2】:

    对于 svg 圈子也有同样的问题。我希望动画逆时针进行。最后通过水平翻转圆圈并使用正虚线偏移来解决它。

    我使用变换来翻转圆圈。

    scale(-1 1)
    

    请参阅 codepen 以获取示例。

    https://codepen.io/rikki404/pen/YzydPJq

    【讨论】:

      【解决方案3】:

      Safari 不支持负短划线集。您需要通过反转路径并让 dashoffset 以另一种方式设置动画来解决此问题。

      【讨论】:

      • 完美,谢谢! (通过将我的描边线变成复合路径并使用 Illustrator 中的“属性”窗口来反转方向,以防其他人在未来遇到这个问题!)
      猜你喜欢
      • 2012-07-10
      • 2015-12-05
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多