Javascript 单击或 10 秒后 jquery 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3234017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery redirect on click or after 10 seconds
提问by Brooke.
I have a spash screen on a website that has a div with the ID of "splash" i'm trying to make the div fade in then if the user clicks on the div it fades out and redircts to the main site. If the user dosen't click it just fades out and redirects after 10 seconds.
我在一个网站上有一个 spash 屏幕,该网站有一个 ID 为“splash”的 div,我试图让 div 淡入,然后如果用户单击 div,它会淡出并重定向到主站点。如果用户没有点击它只会在 10 秒后淡出并重定向。
The timed redirect is working but not the click function.
定时重定向有效,但点击功能无效。
<script type="text/javascript">
$(document).ready(function() {
$('#splash').hide();
$('#splash').fadeIn(1000, function() {
$(this).delay(10000).fadeOut(1000, function() {
window.location = 'http://www.examle.com'; });
$(this).click().fadeOut(1000,function() {
window.location = 'http://www.example.com'; });
});
});
</script>
Any help would be great
任何帮助都会很棒
回答by Josiah
Try this:
尝试这个:
$(document).ready(function() {
$('#splash').hide();
$('#splash').click(function(){
$(this).fadeOut(1000,function() {
window.location = 'http://www.example.com'; });
});
$('#splash').fadeIn(1000, function() {
window.setTimeout ( function() {
$('#splash').fadeOut(1000, function() {
window.location = 'http://www.example.com'; }) }
, 10000);
});
});?
Changes that I've made to the example:
我对示例所做的更改:
I've moved setting the click handler outside the fadeOut function (better practice, IMHO) and I've changed your call to delay() to a setTimeout().
我已经将点击处理程序的设置移到了淡出函数之外(更好的做法,恕我直言),并且我已将您对 delay() 的调用更改为 setTimeout()。
The difference is, delay() will not allow other jQuery code to be executed in the background, while setTimeout() will.
不同之处在于,delay() 不允许其他 jQuery 代码在后台执行,而 setTimeout() 允许。

