我用https://jsfiddle.net/arungeorgez/r9x6vhcb/4/动画从第1点到第2点的一条线。如何将箭头添加到同一行?
<style>
.key-anim1 {
-webkit-animation: Drawpath 1s linear forwards;
animation: Drawpath 1s linear forwards;
stroke-dasharray: 0, 600;
}
@-webkit-keyframes Drawpath {
from {
stroke-dasharray: 0, 600;
}
to {
stroke-dasharray: 600, 600;
}
}
@keyframes Drawpath {
from {
stroke-dasharray: 0, 600;
}
to {
stroke-dasharray: 600, 600;
}
}
</style>发布于 2018-11-16 00:26:32
<animate attributeType="XML"
attributeName="opacity"
from="0" to="1"
dur=".08s" begin=".23s"
repeatCount="0" fill="freeze"
/>似乎对你的情况产生了不错的效果,因为箭头很小。然而,对于更精细的解决方案,可以使用<animateTransform>或<animateMotion>,而不是<animate>,这取决于情况。
这是SMIL动画的规范。
虽然CSS动画的效果很容易实现(最后,我只是上面的opacity动画),但我倾向于为<svg>推荐SMIL动画,因为它们为控制动画的不同方面提供了更多的选项,远远优于CSS选项,IMHO。
function makeSVG(tag, attrs) {
var el = document.createElementNS('http://www.w3.org/2000/svg', tag);
for (var k in attrs)
el.setAttribute(k, attrs[k]);
return el;
}
$(document).ready(function() {
var line = makeSVG('line', {
id: "-",
class: "key-anim1",
x1: 20,
y1: 20,
x2: 120,
y2: 120,
stroke: 'black',
'stroke-width': 2,
'marker-end': "url(#arrow)"
});
document.getElementById("svg").appendChild(line);
});.key-anim1 {
-webkit-animation: Drawpath 1s linear forwards;
animation: Drawpath 1s linear forwards;
stroke-dasharray: 0, 600;
}
@-webkit-keyframes Drawpath {
from {
stroke-dasharray: 0, 600;
}
to {
stroke-dasharray: 600, 600;
}
}
@keyframes Drawpath {
from {
stroke-dasharray: 0, 600;
}
to {
stroke-dasharray: 600, 600;
}
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<svg height="600" width="600" id="svg">
<defs>
<marker id="arrow" markerWidth="10" markerHeight="10" refX="0" refY="3" orient="auto" markerUnits="strokeWidth" opacity="0">
<path d="M0,0 L0,6 L9,3 z" fill="#000" />
<animate attributeType="XML" attributeName="opacity" from="0" to="1" dur=".08s" begin=".23s" repeatCount="0" fill="freeze" />
</marker>
</defs>
</svg>
注意:微调任何动画的简单方法是将速度降低10倍。这样你就能使它变得完美,并且在你对它的表现满意10倍之后再增加它。有时,当你加快速度的时候,你需要做一些小的调整,从技术上来说是正确的,以平衡视觉错觉(但这往往是在遥远的“无形细节”的土地上)。
如果你想让制造者可见并沿着线移动,你需要去掉达沙雷(因为现在你的线条从动画的开始到结束都有相同的长度,但是它是用虚线画的,你只是在移动虚线中的空隙,看起来它在增长)。相反,您需要,使其在最初的短,并动画它变得更长。
下面是一个例子:
<svg height="600" width="600" id="svg">
<defs>
<marker id="arrow" markerWidth="10" markerHeight="10" refX="0" refY="3" orient="auto" markerUnits="strokeWidth">
<path d="M0,0 L0,6 L9,3 z" fill="#000" opacity="0">
<animate attributeType="XML" attributeName="opacity" from="0" to="1" dur=".1s" repeatCount="0" fill="freeze" />
</path>
</marker>
</defs>
<line id="-" x1="20" y1="20" x2="21" y2="21" stroke="black" stroke-width="2" marker-end="url(#arrow)">
<animate attributeType="XML" attributeName="x2" from="21" to="120" dur="1s" repeatCount="0" fill="freeze" />
<animate attributeType="XML" attributeName="y2" from="21" to="120" dur="1s" repeatCount="0" fill="freeze" />
</line>
</svg>
https://stackoverflow.com/questions/53329341
复制相似问题