【发布时间】:2020-02-16 06:45:10
【问题描述】:
我想知道如何使用 javascript 而不是 HTML 为 SVG 路径设置动画。目前,我发现了几篇关于 javascript 动画的溢出文章,并在 jQuery 中找到了许多在 javascript 中更改属性的文章。
我找到的链接: Fill color SVG path with animation Animate SVG path via javascript
有谁知道我如何将这些技术应用到下面的路径中,因为它适用于 HTML,但我想控制动画的持续时间。
<svg width="300" height="300">
<defs>
<linearGradient id="left-to-right">
<stop offset="0" stop-color="#ff0000">
<animate dur="2s" attributeName="offset" opacity= 0.1 fill="freeze" from="0" to="1" />
</stop>
<stop offset="0" stop-color="#fff" stop-opacity= "0.1">
<animate dur="2s" attributeName="offset" opacity= 0.1 fill="freeze" from="0" to="1" />
</stop>
</linearGradient>
</defs>
<path id="myPath"
d="M 280 210 Q 237 144 180 140 Q 120 144 80 210 L 280 210 "
stroke="black" fill="url(#left-to-right)" />
</svg>
问题是javascript无法正常工作
$(function(){
$(this).animate(
{
textIndent: 1,
}, {
duration: 3000,
step: function ( now, fx ) {
var from = 0,
to = 700,
r = from + to * ( now - fx.start ) / ( fx.end - fx.start );
$("#left-to-right").attr( "from", 0);
$("#left-to-right").attr( "from", 1);
},
complete: function () {
$("#myPath").attr("fill", "url(#left-to-right)");
}
}
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<svg width="300" height="300">
<defs>
<linearGradient id="left-to-right">
<stop offset="0" stop-color="#ff0000">
<animate dur="2s" attributeName="offset" opacity= 0.1 fill="freeze" from="0" to="1" />
</stop>
</linearGradient>
</defs>
<path id="myPath"
d="M 280 210 Q 237 144 180 140 Q 120 144 80 210 L 280 210 "
stroke="black" fill="url(#left-to-right)" />
</svg>
我现在必须如何更改 javascript、fx 和属性,以使动画像使用当前 HTML 一样工作?
【问题讨论】:
标签: javascript jquery html css svg