我在一个网站上有一个 spash 屏幕,它有一个 ID 为“splash”的 div 我试图让 div 淡入然后如果用户点击 div 它淡出并重定向到主站点。如果用户不点击,它就会淡出并在 10 秒后重定向。
定时重定向有效,但点击功能无效。
<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>
任何帮助都会很棒
最佳答案
试试这个:
$(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);
});
});
我对示例所做的更改:
我已将点击处理程序设置在 fadeOut 函数之外(更好的做法,恕我直言),并且我已将您对 delay() 的调用更改为 setTimeout()。
区别在于,delay() 不允许在后台执行其他 jQuery 代码,而 setTimeout() 允许。
关于javascript - jquery 重定向点击或 10 秒后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3234017/