简体   繁体   English

点击或10秒后jquery重定向

[英]jquery redirect on click or after 10 seconds

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. 我在一个网站上有一个spash屏幕,其中有一个ID为“splash”的div我试图使div淡入然后如果用户点击它淡出的div并重新指向主站点。 If the user dosen't click it just fades out and redirects after 10 seconds. 如果用户没有点击它就会淡出并在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 任何帮助都会很棒

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(). 我已经将点击处理程序设置在fadeOut函数之外(更好的做法,恕我直言),并且我已将对delay()的调用更改为setTimeout()。

The difference is, delay() will not allow other jQuery code to be executed in the background, while setTimeout() will. 区别在于,delay()不允许在后台执行其他jQuery代码,而setTimeout()则会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM