2014-01-16 60 views
2

我有下面一段代码,它在将URL添加到工作良好的URL中时移动到屏幕上。我现在需要添加一段代码,然后在5秒后移回。我注意到有一个延迟功能,但我不知道如何将它添加到代码中。谁能帮忙?非常感谢!使用JavaScript自动移动div

$(document).ready(
function() { 
    if (document.URL.indexOf("?added") >= 0) { 
     $('#popout-left-menu-container') 
      .animate({ 
       'right': '2px' 
      }, 300); 
    }; 
}); 
+0

可能你应该参考这个http://api.jquery.com/delay/ –

回答

1

您可以使用setTimeout函数来延迟javascript中的某些内容。也许是这样的:

$('#popout-left-menu-container').animate({'right':'2px'},300); 

setTimeout(function(){ 
    //This is animation that runs after 5 seconds. You can use it to move the block back. 
    //You have to set your parameters yourself here 
    $('#popout-left-menu-container').animate({'right':'0px'},300); 
}, 5000); 
1
$(document).ready(
function() { 
    if (document.URL.indexOf("?added") >= 0) { 
     setTimeout(function(){ 
      $('#popout-left-menu-container') 
       .animate({ 
        right:'2px' 
       },300); 
     },5000); 
    }; 

}); 
0

你应该.delay()做到这一点。

$("query").animate(firstAnimation, firstDuration).delay(milliseconds).animate(secondAnimation, secondDuration);