2014-02-09 54 views
0
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); 

我需要一個按鈕,你按它,循環將會去一次。就像一個攻擊動畫,我按下一個按鈕,圖像將攻擊另一個圖像。需要幫助:動畫 - 按鈕點擊 - >後退和前後,一次

+0

你的回調再次觸發動畫功能,所以看起來像這樣只會不斷循環。 – VIDesignz

+0

函數調用應該是animatethis(「#q1」,1000),因爲在函數本身已經有了$() – VIDesignz

回答

0

在這裏你去的人....讓我知道如果你有任何問題!

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); 
});