css - iOS 上的动画 stroke-dashoffset

标签 css animation svg

因此,我通过增加 stroke-dashoffset 值来为这个 SVG Logo 设置动画

svg {
  position: absolute; top: 50%; left: 50%;
  transform: translateX(-50%) translateY(-50%);
  width: 150px;
}

.cls-1,
.cls-2 {
  fill:none;
  stroke:#a9a9a9;
  stroke-linecap:round;
  stroke-linejoin:round;
  stroke-width:10px;
}

.cls-1 {
  stroke-dasharray: 496;
  stroke-dashoffset: -496;
  animation: firstLine 2s ease-out 0s infinite normal;
}

.cls-2 {
  stroke-dasharray: 458;
  stroke-dashoffset: -458;
  animation: secondLine 2s ease-out 0s infinite normal;
}

@keyframes firstLine {
  0% { stroke-dashoffset: -496; }
  40% { stroke-dashoffset: 0; }
  60% { stroke-dashoffset: 0; }
  85% { stroke-dashoffset: 496; }
  100% { stroke-dashoffset: 496; }
}

@keyframes secondLine {
  0% { stroke-dashoffset: -458; }
  45% { stroke-dashoffset: 0; }
  60% { stroke-dashoffset: 0; }
  90% { stroke-dashoffset: 458; }
  100% { stroke-dashoffset: 458; }
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200.17 135"><path class="cls-1" d="M132.67,28.44a39.06,39.06,0,0,1,0,78.12H113.22V59.11A54.11,54.11,0,0,0,5,59.11v57.35a13.54,13.54,0,0,0,27.08,0v-9.9h27"/><path class="cls-2" d="M113.63,5h19a62.5,62.5,0,0,1,0,125H102.44a16.29,16.29,0,0,1-16.3-16.29V59.11a27,27,0,0,0-54.06,0V79.89h27"/></svg>

在桌面浏览器上打开时,一切正常。安卓也一样。但是在 iOS 上,动画就大错特错了。是否有一些我不知道的 iOS 特定错误 stroke-dashoffset

最佳答案

长期以来,我一直在寻找一种解决方案,即如何将负的 stroke-dashoffset 值替换为正值,以规避 safari 施加的限制。

给出了动画一行的解释。对于第二行,计算类似。

  • 线条的总长度为496 px;因此, 的值 stroke-dashoffset = "496"完全隐藏线条。
  • 使用 double 值 stroke-dashoffset ="992" 画线
  • 使用三重值 stroke-dashoffset ="1488 ",线条被删除 再次

CSS 解决方案

svg {
  position: absolute; top: 50%; left: 50%;
  transform: translateX(-50%) translateY(-50%);
  width: 150px;
}

.cls-1,
.cls-2 {
  fill:none;
  stroke:#a9a9a9;
  stroke-linecap:round;
  stroke-linejoin:round;
  stroke-width:10px;
}
.cls-1 {
  stroke-dasharray: 496;
  stroke-dashoffset: 0;
  animation: firstLine 2s ease-out 0s infinite normal;
}

.cls-2 {
  stroke-dasharray: 458;
  stroke-dashoffset: 0;
  animation: secondLine 2s ease-out 0s infinite normal;
}

@keyframes firstLine {
  0% { stroke-dashoffset: 496; }
  40% { stroke-dashoffset: 992; }
  60% { stroke-dashoffset: 992; }
  85% { stroke-dashoffset: 1488; }
  100% { stroke-dashoffset: 1488; }
}

@keyframes secondLine {
  0% { stroke-dashoffset: 458; }
  45% { stroke-dashoffset: 916; }
  60% { stroke-dashoffset: 916; }
  90% { stroke-dashoffset: 1374; }
  100% { stroke-dashoffset: 1374; }
} 
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200.17 135">
<path class="cls-1 " d="M132.67,28.44a39.06,39.06,0,0,1,0,78.12H113.22V59.11A54.11,54.11,0,0,0,5,59.11v57.35a13.54,13.54,0,0,0,27.08,0v-9.9h27" >
</path>
  <path class="cls-2" d="M113.63,5h19a62.5,62.5,0,0,1,0,125H102.44a16.29,16.29,0,0,1-16.3-16.29V59.11a27,27,0,0,0-54.06,0V79.89h27" > 
</path>
 
</svg>

Logo 周围带有阴影的选项

添加阴影

<defs>
        <filter id="shadow" x="-20%" y="-20%" width="200%" height="200%">
            <feDropShadow dx="4" dy="8" stdDeviation="4"/>
        </filter>
    </defs> 

body {
background: rgb(144,210,152);
background: linear-gradient(286deg, rgba(144,210,152,1) 29%, rgba(236,234,154,1) 69%);
}
svg {
  position: absolute; top: 50%; left: 50%;
  transform: translateX(-50%) translateY(-50%);
  width: 150px;
}

.cls-1,
.cls-2 {
  fill:none;
  stroke:#a9a9a9;
  stroke-linecap:round;
  stroke-linejoin:round;
  stroke-width:10px;
  filter:url(#shadow);
}
.cls-1 {
  stroke-dasharray: 496;
  stroke-dashoffset: 0;
  animation: firstLine 4s ease-out 0s infinite normal;
}

.cls-2 {
  stroke-dasharray: 458;
  stroke-dashoffset: 0;
  animation: secondLine 4s ease-out 0s infinite normal;
}

@keyframes firstLine {
  0% { stroke-dashoffset: 496; }
  40% { stroke-dashoffset: 992; }
  60% { stroke-dashoffset: 992; }
  85% { stroke-dashoffset: 1488; }
  100% { stroke-dashoffset: 1488; }
}

@keyframes secondLine {
  0% { stroke-dashoffset: 458; }
  45% { stroke-dashoffset: 916; }
  60% { stroke-dashoffset: 916; }
  90% { stroke-dashoffset: 1374; }
  100% { stroke-dashoffset: 1374; }
} 
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 220.17 150">
<defs>
        <filter id="shadow" x="-20%" y="-20%" width="200%" height="200%">
            <feDropShadow dx="4" dy="8" stdDeviation="4"/>
        </filter>
    </defs> 
 	
<path class="cls-1 " d="M132.67,28.44a39.06,39.06,0,0,1,0,78.12H113.22V59.11A54.11,54.11,0,0,0,5,59.11v57.35a13.54,13.54,0,0,0,27.08,0v-9.9h27" >
</path>
  <path class="cls-2" d="M113.63,5h19a62.5,62.5,0,0,1,0,125H102.44a16.29,16.29,0,0,1-16.3-16.29V59.11a27,27,0,0,0-54.06,0V79.89h27" > 
</path> 

</svg>

SVG解决方案

svg {
  position: absolute; top: 50%; left: 50%;
  transform: translateX(-50%) translateY(-50%);
  width: 150px;
}

.cls-1,
.cls-2 {
  fill:none;
  stroke:#a9a9a9;
  stroke-linecap:round;
  stroke-linejoin:round;
  stroke-width:10px;
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200.17 135">
<path class="cls-1 " d="M132.67,28.44a39.06,39.06,0,0,1,0,78.12H113.22V59.11A54.11,54.11,0,0,0,5,59.11v57.35a13.54,13.54,0,0,0,27.08,0v-9.9h27" stroke-dasharray="496,496" stroke-dashoffset="496">
  <animate id="an_offset"
    attributeName="stroke-dashoffset"
	begin="0s"
	dur="3s"
	values="496;992;992;1488"
	fill="freeze"
	repeatCount="indefinite"  />
</path>
  <path class="cls-2" d="M113.63,5h19a62.5,62.5,0,0,1,0,125H102.44a16.29,16.29,0,0,1-16.3-16.29V59.11a27,27,0,0,0-54.06,0V79.89h27" stroke-dasharray="458,458" stroke-dashoffset="458"> 
    <animate id="an_offset2"
	 attributeName="stroke-dashoffset"
	 begin="0s"
	 dur="3s"
	 values="458;916;916;1374"
	 fill="freeze"
	 repeatCount="indefinite" />
</path>
 
</svg>

附加示例
从每条线的中点开始绘制

要实现动画,更改stroke-dasharray属性的参数

总行长为496px,一半是`248px

  • 因为参数的值按顺序表示:
    0 - 行248 - 空格 0 - 行248 - 空格
    因此,写stroke-dasharray = "0,248 0,248"会完全隐藏该行
  • 使用 stroke-dasharray = "0,0 496,0" 线条将完全可见。

svg {
  position: absolute; top: 50%; left: 50%;
  transform: translateX(-50%) translateY(-50%);
  width: 150px;
}

.cls-1,
.cls-2 {
  fill:none;
  stroke:#a9a9a9;
  stroke-linejoin:round;
  stroke-width:10px;
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200.17 135">
<path class="cls-1 " d="M132.67,28.44a39.06,39.06,0,0,1,0,78.12H113.22V59.11A54.11,54.11,0,0,0,5,59.11v57.35a13.54,13.54,0,0,0,27.08,0v-9.9h27" stroke-dasharray="496,496" stroke-dashoffset="496">
  <animate id="an_array"
    attributeName="stroke-dasharray"
	begin="0s"
	dur="4s"
	values="0,248 0,248;0,0 496,0;0,0 496,0;0,248 0,248"
	fill="freeze"
	repeatCount="indefinite"  />
</path>
  <path class="cls-2" d="M113.63,5h19a62.5,62.5,0,0,1,0,125H102.44a16.29,16.29,0,0,1-16.3-16.29V59.11a27,27,0,0,0-54.06,0V79.89h27" stroke-dasharray="458,458" stroke-dashoffset="458"> 
    <animate id="an_array2"
	  attributeName="stroke-dasharray"
	  begin="0s"
	  dur="4s"
	  values="0,229 0,229;0,0 458,0;0,0 458,0;0,229 0,229"
	  fill="freeze"
	  repeatCount="indefinite" />
</path> 
 

关于css - iOS 上的动画 stroke-dashoffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57964904/

相关文章:

javascript - 固定定位点击后自行重置

html - Bootstrap 3 - 如何在导航中获取标签栏

c++ - 稳定 QWidget::paintEvent() 调用频率

css - 为什么 CSS 和 SVG 径向渐变不匹配?

css - 你可以让 svg 图像显示为背景......即使在 Internet Explorer 中?

html - 在左列和右列中的图像上 float 文本 block

css - 在 Firefox 中,svg 掩码不缩放

ios - 如何在 IOS 中为 SVG 路径设置动画?

javascript - HTML/CSS 等到页面加载完毕

javascript - 如何在悬停时停止 SVG animateMotion?