iOS 上的动画 stroke-dashoffset
Posted
技术标签:
【中文标题】iOS 上的动画 stroke-dashoffset【英文标题】:Animated stroke-dashoffset on iOS 【发布时间】:2020-01-17 18:23:38 【问题描述】:所以,我通过增加 stroke-dashoffset
值来动画化这个 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;
.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
?
【问题讨论】:
Safari 不支持将负数作为 stroke-dashoffset 值。这是一个长期存在的错误,其他 UA 对负数没有问题。 @RobertLongson 哦,我期待一个错误,但不是那样的。谢谢:) 【参考方案1】:长期以来,我一直在寻找一种解决方案,如何用正值替换负的 stroke-dashoffset 值,以规避 safari 施加的限制。
给出了动画一行的解释。对于第二行,计算类似。
行总长度为496 px
;因此,
stroke-dashoffset = "496"
的值完全隐藏了该行。
用双精度值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>
标志周围有阴影的选项
添加阴影
<defs>
<filter id="shadow" x="-20%" y="-20%" >
<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%" >
<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 - line
,248 - space
0 - line
,248 - space
.
因此,写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>
【讨论】:
嗨,对不起,由于我已经用我的回答结束了这个问题,我没有注意通知。我的错,解决这个问题,谢谢:) @Axiol 感谢您提出有趣的问题。解决它很有趣。【参考方案2】:正如@RobertLongson 所述,这是一个 Safari 错误,不支持 stroke-dashiffset
的负值
【讨论】:
以上是关于iOS 上的动画 stroke-dashoffset的主要内容,如果未能解决你的问题,请参考以下文章
CSS 动画 (transform: rotateY(180deg);) 完美运行,除了 iOS 上的 Chrome