【发布时间】:2016-06-08 05:13:32
【问题描述】:
我目前正在制作 svg 动画。
我有一个 SVG 元素,它遵循带有 animateMotion 属性的路径。
我的形状:
<g id="dart" transform="scale(-1,1) translate(-587, -145)">
<path d="..." />
</g>
我的路:
<path id="motionPath" fill="none" stroke="none" stroke-miterlimit="10" d="..."/>
我的动画运动:
<animateMotion xlink:href="#dart" dur="1s" begin="0s" fill="freeze" rotate="auto"><mpath xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#motionPath"></mpath></animateMotion>
完美运行。但是,当页面加载后,它会在 2 秒后开始移动。我想在 JS 事件上启动动画。实际上,我需要在页面中向下滚动以查看 SVG,并且当他可见时动画开始。
JS:
var pos = $("#dart").offset().top;
$(window).scroll(function(){
var scrollTop=$(window).scrollTop();
if(scrollTop>=pos){
/* --- start animation here with injecting --- */
var motion=document.createElementNS("http://www.w3.org/2000/svg","animateMotion");
motion.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", "#dart");
motion.setAttributeNS("http://www.w3.org/2000/svg", "dur", "1s");
motion.setAttributeNS("http://www.w3.org/2000/svg", "begin", "0s");
motion.setAttributeNS("http://www.w3.org/2000/svg", "fill", "freeze");
motion.setAttributeNS("http://www.w3.org/2000/svg", "rotate", "auto");
var mpath = document.createElementNS("http://www.w3.org/2000/svg","mpath");
mpath.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", "#motionPath");
motion.appendChild(mpath);
document.getElementById("target").appendChild(motion);
}
});
我尝试注入(使用 createElementNS)<animateMotion> 或更改开始的值以启动事件动画,但它不起作用。
如果有人有想法...
【问题讨论】:
标签: html svg path jquery-animate