setTimeout,淡出并转到url [复制]

问题描述 投票:0回答:3

这个问题在这里已有答案:

我试图创建一个功能,在一段时间后花在特定页面上,没有任何用户操作,它会将他重定向到另一个页面,但具有淡出效果。一切都很好,除了当前页面不淡出,只是简单地转到新页面。这是我的代码:

setTimeout(function(){
$('body').fadeOut('slow');
window.location.href = "index.html";
}, 6000);
jquery settimeout
3个回答
2
投票

fadeOut方法接受另一个参数,该参数是动画完成时执行的callback函数,因此,您可以在duration参数旁边传递包含重定向语句的函数:

setTimeout(function() {
  $('body').fadeOut('slow', function() {
    window.location.href = "index.html";
  });
}, 6000);

希望我进一步推动你。


2
投票

将位置更改放在fadeOut完成后运行的回调中。

setTimeout(function(){
    $('body').fadeOut('slow', function() {
        window.location.href = "index.html";
    });
}, 6000);

这可能会使你的setTimeout多余。你可能只想要:

$('body').fadeOut('slow', function() {
    window.location.href = "index.html";
});

0
投票

你应该在文档或html部分使用fadeout():

$(document).fadeOut();

要么

$("html").fadeOut();

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.