【问题标题】:Need Help: Animation - Button Click -> Back and Forth, Once需要帮助:动画 - 按钮单击 -> 来回,一次
【发布时间】:2014-03-06 22:20:23
【问题描述】:
function animatethis(targetElement, speed) {
$(targetElement).animate({
    marginLeft: "+=250px"
}, {
    duration: speed,
    complete: function () {
        targetElement.animate({
            marginLeft: "-=250px"
        }, {
            duration: speed,
            complete: function () {
                animatethis(targetElement, speed);
            }
        });
    }
});
};

animatethis($('#q1'), 1000);

我需要一个按钮,你按下它,循环就会执行一次。就像一个攻击动画,我按下一个按钮,图像将攻击另一个图像。

【问题讨论】:

  • 您的回调再次触发了动画函数,所以看起来这将继续循环。
  • 并且函数调用应该只是 animatethis("#q1" , 1000) 因为你已经在函数本身中有 $()

标签: javascript jquery animation


【解决方案1】:

给你,伙计……如果你有任何问题,请告诉我!

Working Example

// Animation Function
function animatethis(targetElement, speed) {    
    $(targetElement).animate({"margin-left" : "+=50px"}, speed, function(speed){
        $(this).animate({"margin-left" : "-=50px"}, speed);
    }); 
};

//Animation Trigger
$('body').on('click', 'button', function(){
    animatethis('#q1', 250);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2011-09-02
    • 1970-01-01
    • 2018-02-04
    • 2020-11-12
    • 1970-01-01
    相关资源
    最近更新 更多