【问题标题】:Animation of an SVG object with jQuery使用 jQuery 制作 SVG 对象的动画
【发布时间】:2013-05-30 15:49:14
【问题描述】:

我正在尝试使用 jQuery 为 svg 对象设置动画。 动画应该是这样的:

  • 单击主要对象 -> 向上平移主要对象。
  • 单击另一个对象 -> 从实际位置开始以另一种方式平移主要对象。

看看这个http://jsfiddle.net/MaxMarkson/khZqW/2/

$('#ellipseRed').click(function() {
    $(this)
    .css({"min-height": 0})
    .animate(
        {"min-height": -150},
        {duration: 1000,
         step: function(top){
             this.setAttribute("transform", "translate(0,"+top+")");
         }
        }
    );
});

$('#ellipseBlue').click(function() {
    // Getting
    var xforms = $('#ellipseRed')[0].getAttribute('transform');
    var parts  = /translate\(\s*([^\s,)]+)[ ,]([^\s,)]+)/.exec(xforms);
    var firstX,firstY;
    if(parts == null){
        $('#ellipseRed')[0].setAttribute('transform','translate(0,0)');
        firstX = 0;
        firstY = 0;
    }
    else{
        firstX = parts[1];
        firstY = parts[2];
    }
    // Setting
    //
    var animation = {};
    animation.x = firstX + 200;
    animation.y = firstY - 100;

    $('#ellipseRed')
    .css({"min-height": 0})
    .css({"left":0})
    .animate(
        {"min-height": animation.y},
        {"left": animation.x},
        {duration: 1000,
         step: function(top, left){
             this.setAttribute("transform", "translate("+left+","+top+")");
         }
        }
    );
});

主要对象是红色椭圆,通过单击它应用的翻译工作正常,其他动画不起作用,我不知道为什么。

谢谢!

【问题讨论】:

  • 据我所知,你得到一个错误,因为你的第二个函数使用 DOM 对象本身,而不是 jQuery 对象,使用 $('#ellipseRed').css() 而不是 $ ('#ellipseRed')[0].
  • 由于.getAttribute() 和.setAttribute() 方法,我使用了DOM 对象。无论如何,带有attr() 方法的jquery 对象仍然不起作用
  • 我不知道你想要什么样的效果——但问题在于你使用的动画逻辑,我对你的小提琴做了一些修改,正如你所见,它可以工作。 jsfiddle.net/MaxMarkson/khZqW/2
  • 可能你没有保存你的修改...代码是一样的。效果很简单:点击蓝色的椭圆,红色的椭圆应该移动到页面的右上角,从当时的位置移动,而不是从加载页面时椭圆的位置移动。
  • 对不起,你是对的,这里是:jsfiddle.net/axelmichel/khZqW/5

标签: jquery svg jquery-svg


【解决方案1】:

试试这个:- http://jsfiddle.net/adiioo7/khZqW/8/

JS

$('#ellipseRed').click(function () {
    $(this)
        .css({
        "min-height": 0
    })
        .animate({
        "min-height": -150
    }, {
        duration: 1000,
        step: function (top) {
            this.setAttribute("transform", "translate(0," + top + ")");
        }
    });
});

$('#ellipseBlue').click(function () {
    // Get the value of the transform attribute
    var ellipse_red = $('#ellipseRed');
    var xforms = ellipse_red.attr('transform');
    var parts = /translate\(\s*([^\s,)]+)[ ,]([^\s,)]+)/.exec(xforms);
    var firstX, firstY;
    // If the transform attribute doesn't exist then add it.
    if (parts == null) {
        ellipse_red.attr('transform', 'translate(0,0)');
        firstX = 0;
        firstY = 0;
    }
    // Otherwise take the values read.
    else {
        firstX = parseInt(parts[1]);
        firstY = parseInt(parts[2]);
    }
    // Set the arrive point
    var animation = {};
    animation.x = firstX + 200;
    animation.y = firstY - 100;

    // Try to animate!
    ellipse_red.animate({
        "min-height": -200
    }, {
        duration: 1000,
        step: function (top) {
            this.setAttribute("transform", "translate(" + -top + "," + firstY + ")");
        }
    });
});

【讨论】:

  • 你真的很近,但是当点击蓝色时,红色椭圆只向右移动......似乎忽略了垂直移动
  • 好的,最后一件事:可以将红色椭圆从它到达的最后位置移动吗?如果您看到最后一个 axel.michel 的小提琴,椭圆会到达正确的最终位置,但它是从原始起点开始的。在您的小提琴中,椭圆正在正确移动,但会在相同的位置开始和结束。希望你能理解,我的英语不好,我很难解释自己。
  • 现在我们真的很接近了!!!最终效果应该与jsfiddle.net/MaxMarkson/khZqW/11 非常相似,但即使在 y 轴上也有流畅的运动。非常感谢你给我的帮助:)
  • @MaxMarkson 请接受您的问题的适当答案。
  • 您的回答让我走上了正确的道路,但并不完全正确。正确的是我的,但我不能在 2 天内接受,所以我必须等待。
【解决方案2】:

好的,我已经达到了目标: http://jsfiddle.net/MaxMarkson/khZqW/12/

$('#ellipseRed').click(function () {
    $(this)
        .css({
        "min-height": 0
    })
        .animate({
        "min-height": -150
    }, {
        duration: 1000,
        step: function (top) {
            this.setAttribute("transform", "translate(0," + top + ")");
        }
    });
});

$('#ellipseBlue').click(function () {
    // Get the value of the transform attribute
    var ellipse_red = $('#ellipseRed');
    var xforms = ellipse_red.attr('transform');
    var parts = /translate\(\s*([^\s,)]+)[ ,]([^\s,)]+)/.exec(xforms);
    var firstX, firstY;
    // If the transform attribute doesn't exist then add it.
    if (parts == null) {
        ellipse_red.attr('transform', 'translate(0,0)');
        firstX = 0;
        firstY = 0;
    }
    // Otherwise take the values read.
    else {
        firstX = parseInt(parts[1]);
        firstY = parseInt(parts[2]);
    }
    // Set the arrive point
    var animation = {};
    var delta = {};
    delta.x = 20;
    delta.y = -50;
    animation.x = firstX + delta.x;
    animation.y = firstY + delta.y;

    // Try to animate!
    ellipse_red.animate({
        "increment": animation.x,
    }, {
        duration: 1000,
        step: function (top) {
            this.setAttribute("transform", "translate(" + top + "," + (top*(delta.y/delta.x)) + ")");
        }
    });
});

我不确定这是不是最好的解决方案,但它有效:我只使用一个参数并使用“转换参数”评估每个轴上的运动,在这种情况下,y 轴的两个增量增量之间的划分和x 轴为 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    相关资源
    最近更新 更多