jQuery动画SVG元素

ram*_*esh 9 jquery svg jquery-svg jquery-animate

我的应用程序中有一个svg.喜欢

<svg id="gt" height="450" width="300" xmlns="http://www.w3.org/2000/svg">
<image id="1_dice" x="0" y="420" height="30" width="30" xlink:href="images/1_coin.png" />
</svg>
Run Code Online (Sandbox Code Playgroud)

我有一个名为'1_dice'的svg元素.在HTML按钮单击中,我希望根据参数为元素设置动画.喜欢

$('#btn').click(function(){
     $('#1_dice').animate({'x':200},2000);
});
Run Code Online (Sandbox Code Playgroud)

我尝试了这个,但这不起作用......

Syg*_*ral 10

没有插件就有可能,但它涉及到一个技巧.问题是它x不是css属性而是属性,而jQuery.animate只能动画css属性.但您可以使用该step参数为动画指定自己的自定义行为.

foo 是一个不存在的属性,我们在step函数中使用它的动画值.

$('#btn').click(function(){
    $('#dice_1').animate(
        {'foo':200},
        {
            step: function(foo){
                 $(this).attr('x', foo);
            },
            duration: 2000
        }
    );
});
Run Code Online (Sandbox Code Playgroud)


Arj*_*Raj 7

jQuery animate用于动画HTML元素.对于SVG,您必须尝试jQuery SVG插件.请点击链接 - http://keith-wood.name/svg.html

  • 更确切地说,jQuery动画用于动画CSS属性.一些CSS可以应用于SVG元素,例如不透明度,填充,笔触.有关解释,请参阅[本文](http://www.smashingmagazine.com/2014/11/styling-and-animating-svgs-with-css/). (2认同)