您可以将a stroke-dashoffset
设置为行并将其设置为0的动画。为了计算和的值stroke-dasharray
,stroke-dashoffset
我使用了getTotalLength()方法来计算stroke-dasharray
and 的值stroke-dashoffset
。希望对您有所帮助。
svg{background:#efefef;width:100vh}
rect,line{stroke:black; fill:none}
#om{stroke-dasharray:94.34px;
stroke-dashoffset:94.34px;
animation: om 1s linear forwards;}
@keyframes om {
to {
stroke-dashoffset: 0;
}
}
Run Code Online (Sandbox Code Playgroud)
<svg viewBox = "0 0 100 70">
<rect x="10" y="10" width="80" height="50" />
<line id="om" x1="90" y1="60" x2="10" y2="10" />
</svg>
Run Code Online (Sandbox Code Playgroud)
这是两条动画线,其中m变为o,l变为n:只需将x1的值更改为x2,反之亦然。y也一样。这将改变线的方向。
svg{background:#efefef;width:100vh}
rect,line{stroke:black; fill:none}
#mo,#ln{stroke-dasharray:94.34px;
stroke-dashoffset:94.34px;
animation: om 1s linear forwards;}
@keyframes om {
to {
stroke-dashoffset: 0;
}
}
Run Code Online (Sandbox Code Playgroud)
<svg viewBox = "0 0 100 70">
<rect x="10" y="10" width="80" height="50" />
<!--<line id="om" x1="90" y1="60" x2="10" y2="10" />
<line id="nl" x1="90" y1="10" x2="10" y2="60" />-->
<line id="mo" x2="90" y2="60" x1="10" y1="10" />
<line id="ln" x2="90" y2="10" x1="10" y1="60" />
</svg>
Run Code Online (Sandbox Code Playgroud)
我同时使用相同的动画#om
和#nl