SVG直线动画CSS

时间:2017-12-14 05:18:14

标签: html css svg svg-animate

我正在努力实现这一点(下图)在网站中使用SVG路径进行动画制作。我看到了这个https://css-tricks.com/svg-line-animation-works/并想尝试一下。但我不知道如何创建出路径和动画。请帮帮我!感谢

enter image description here

2 个答案:

答案 0 :(得分:3)

您可以使用svg代码进行动画制作。您可以在svg路径上使用CSS动画。

.st0{
fill:none;
stroke:#000000;
stroke-miterlimit:10;
stroke-dasharray: 1000;
stroke-dashoffset: 1000;
-webkit-animation: draw1 4s linear forwards;
animation: draw1 4s linear forwards;
}
.st1{
fill:none;
stroke:#000000;
stroke-miterlimit:10;
stroke-dasharray: 200;
stroke-dashoffset: 200;
-webkit-animation: draw2 3s linear 2s forwards;
animation: draw2 3s linear 2s forwards;
}
  
@-webkit-keyframes draw1{
  to {stroke-dashoffset: 0;}
}
@keyframes draw1{
  to {stroke-dashoffset: 0;}
}
@-webkit-keyframes draw2{
  to {stroke-dashoffset: 0;}
}
@keyframes draw2{
  to {stroke-dashoffset: 0;}
}
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 viewBox="0 0 614 53" style="enable-background:new 0 0 614 53;" xml:space="preserve">
<polyline class="st0" points="0.5,53 0.5,20.7 613.5,20.7 613.5,53 "/>
<line class="st1" x1="307" y1="53" x2="307" y2="0"/>
</svg>

答案 1 :(得分:1)

SVG解决方案

动画使用属性<stroke-dashoffset> - 来自行首的缩进。在<stroke-dashoffset>的最大值处,可见线为零  使用<stroke-dashoffset ="0">的值,该行获取最大大小。

有必要准确计算线条的长度,以避免动画中出现不可预测的影响。

在此示例中,<line>的长度为53px,<polyline>的长度为680px。

&#13;
&#13;
<style>
.st0{fill:none;stroke:#000000;stroke-miterlimit:10; stroke-dasharray: 680; stroke-dashoffset: 680;} 
.st1{fill:none;stroke:#000000;stroke-miterlimit:10; stroke-dasharray: 53; stroke-dashoffset: 53;}  
 </style>
<svg version="1.1"  viewBox="0 0 614 53" >
 <polyline class="st0" points="0.5,53 0.5,20.7 613.5,20.7 613.5,53 " >
  <animate
    attributeName="stroke-dashoffset"
    from="680"
    to="0"
    dur="2s"
    fill="freeze" />
 </polyline>
<line class="st1" x1="307" y1="53" x2="307" y2="0">
    <animate
      attributeName="stroke-dashoffset"
      from="53"
      to="0"
      dur="2s"
      fill="freeze" />
</line>
 
</svg>
&#13;
&#13;
&#13;

相关问题