iOS上的动画笔划破折号

时间:2019-09-16 21:59:52

标签: css animation svg

因此,我通过增加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>

在桌面浏览器上打开后,一切都很好。与Android相同。但是在iOS上,动画出错了。有我不知道的iOS特定错误stroke-dashoffset吗?

2 个答案:

答案 0 :(得分:3)

很长一段时间以来,我一直在寻找一种解决方案,如何用正值替换负的笔划偏移值,以规避野生动物园施加的限制。

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

  • 该行的总长度为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 - line248 - space 0 - line248 - 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> 
 

答案 1 :(得分:1)

如@RobertLongson所述,这是一个Safari浏览器错误,其中不支持stroke-dashiffset的负值

相关问题