svg animateMotion通过js动态添加无效

如果不加载js直接,直接写在html是正常的。应该如何解决?

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script src="http://code.jquery.com/jquery-1.12.4.js"></script>
    </head>

    <body>
        <div id="map" style="width: 1024px;height:930px;margin: auto;overflow: hidden;">
    <svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="100%" width="100%">
                <!--             <path fill="none" data-uid="1179" id="motionPath-1179-1" stroke="red" d="M940,190Q880,230 870,220Q860,230 850,230Q840,230 830,230Q820,230 810,220Q810,210 830,230Q810,200 810,190" stroke-width="3"></path>
                <image x="-15" y="-30" width="30" height="60" class="cop" id="cop" style="" href="./cop.gif"></image>
                <animateMotion xlink:href="#cop" class="animate" rotate="auto-reverse" dur="8s" fill="freeze">
                    <mpath xlink:href="#motionPath-1179-1"></mpath>
                </animateMotion> -->
    </svg>
        </div>
    </body>
    <script>
    var drawAnimate = function() {
        var svg = $("svg");

        var g = document.createElementNS('http://www.w3.org/2000/svg', 'g');

        var path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
        path.setAttribute("fill", "none");
        path.setAttribute("data-uid", 'motionPath-1179-1');
        path.setAttribute("id", 'motionPath-1179-1');
        path.setAttribute("stroke", 'red');
        path.setAttribute("d", "M" + '940,190Q880,230 870,220Q860,230 850,230Q840,230 830,230Q820,230 810,220Q810,210 830,230Q810,200 810,190');
        path.setAttribute("stroke-width", 3);
        $(g).append(path);

        var image = document.createElementNS('http://www.w3.org/2000/svg', 'image');
        image.setAttribute("x", 0);
        image.setAttribute("y", 0);
        image.setAttribute("width", 30);
        image.setAttribute("height", 60);
        image.setAttribute("class", "cop");
        image.setAttribute("id", "cop");
        image.href.baseVal = "./cop.gif"; 
        $(g).append(image);

        var animate = document.createElementNS('http://www.w3.org/2000/svg', 'animateMotion');
        animate.setAttribute("xlink:href", '#cop');
        animate.setAttribute("class", "animate");
        animate.setAttribute("repeatCount","indefinite");
        animate.setAttribute("rotate", "auto-reverse");
        animate.setAttribute("dur", "8s");
        // animate.setAttribute("begin", "click");
        animate.setAttribute("fill", "freeze");

        var mpath = document.createElementNS('http://www.w3.org/2000/svg', 'mpath');
        mpath.setAttribute("xlink:href", '#motionPath-1179-1');

        $(animate).append(mpath);
        $(g).append(animate);
        svg.append(g);
        return g;
    }
    drawAnimate();
    </script>
阅读 5.6k
2 个回答

请问问题解决没有,我也有同样的问题 T_T

新手上路,请多包涵

找了好久,看到这个问题,简直高兴的不行。结果进来又是没有解决的。不知道楼主解决没有,也没有个响应。我分享下我的解决办法。其实是可以动态添加的,问题是出在xlink:href上面,导致这个没有链接到对应的元素上。原因是这个xlink:href是需要指定命名空间的。所以如下处理下:

var animate = document.createElementNS('http://www.w3.org/2000/svg', 'animateMotion');
animate.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", "#circle_" + this.id);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题