所以,我通过增加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
?
长期以来,我一直在寻找一种解决方案,如何将负笔划线偏移量值替换为正值,以规避野生动物园施加的限制。
给出了对一行进行动画处理的解释。对于第二行,计算类似。
- 直线的总长度是
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%" 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 - 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>
正如@RobertLongson所提到的,这是一个 Safari 错误,其中 is 不支持负值stroke-dashiffset