jquery redirect on click or after 10 seconds

前端 未结 1 1782
庸人自扰
庸人自扰 2021-01-20 00:24

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

1条回答
  •  盖世英雄少女心
    2021-01-20 01:20

    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().

    The difference is, delay() will not allow other jQuery code to be executed in the background, while setTimeout() will.

    0 讨论(0)
提交回复
热议问题