JQuery - Help with .animate and function callback
I am trying the following, but cant get the function to run itself again (I am try开发者_高级运维ing to create some kind of looping animation)
$(document).ready(function() {
//function loopingFunction(){
function loop() {
$('#item').animate({top:'+=100'}, 500, function() {
$('#item').animate({top:'-=100'},500, function(){
loop;
});
});
}
});
Either:
function loop() {
$('#item').animate({top:'+=100'}, 500, function() {
$('#item').animate({top:'-=100'},500, function(){
loop();
});
});
}
Live Example: http://jsfiddle.net/vyef6/
or
function loop() {
$('#item').animate({top:'+=100'}, 500, function() {
$('#item').animate({top:'-=100'},500, loop);
});
}
Live example: http://jsfiddle.net/w92b2/
Explaination: In the first one, you are literally executing the loop
function. Therefore needs the parenthesis. Ine the second you're passing a reference,or callback, to the loop
function in to animate
- therefore it doesnt need the parenthesis.
精彩评论