【问题标题】:Automatically move div with javascript on delay延迟时使用javascript自动移动div
【发布时间】:2014-01-16 09:07:49
【问题描述】:
我有下面的一段代码,当 ? added 在 URL 中时,它会移动到屏幕上,效果很好。我现在需要向它添加一段代码,然后在 5 秒后将其移回。我注意到有一个延迟功能,但我不知道如何将它添加到代码中。任何人都可以帮忙吗?非常感谢!
$(document).ready(
function () {
if (document.URL.indexOf("?added") >= 0) {
$('#popout-left-menu-container')
.animate({
'right': '2px'
}, 300);
};
});
【问题讨论】:
标签:
javascript
jquery
html
css
jquery-animate
【解决方案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);
【解决方案2】:
$(document).ready(
function () {
if (document.URL.indexOf("?added") >= 0) {
setTimeout(function(){
$('#popout-left-menu-container')
.animate({
right:'2px'
},300);
},5000);
};
});
【解决方案3】:
您应该使用.delay()。
$("query").animate(firstAnimation, firstDuration).delay(milliseconds).animate(secondAnimation, secondDuration);