开发者

Jquery fadeIn() wait till clicked

开发者 https://www.devze.com 2023-03-18 23:24 出处:网络
I\'m a little new to jQuery and javascript. On my html, I have a div id=\"test\", under which I have div\'s like #testone,#testtwo,...

I'm a little new to jQuery and javascript. On my html, I have a div id="test", under which I have div's like #testone,#testtwo,...

Now if I click on the button #clickme, the fade effect comes, but goes away very soon, even before it reaches #testthree.

I want it to be there until the user clicks somewhere on the document. Also is there a neater way to do this than to repeat elements?

$( '#clickme' ).click( function() {
    $("#test").fadeIn(function(){
      $("#testone").show().fadeIn("3000", function(){
        $("#testtwo").fadeIn("4500", function(){
          $("#testthree").fadeIn("6000", function(){
        $("#testfour").fadeIn("7500", function(){
          $("#testfive").fadeIn("9000", function(){
            return false;
              });
            });
          });
        });
      });   
    });
});

$(document).click( funct开发者_JAVA技巧ion() {
    $( '#test' ).hide(1000);
    return false; 
});


Try this, assuming all your #test, #testone, #testtwo divs are siblings inside a parent container:

function fade_in_recursive(e,duration,callback)
{
    $(e).fadeIn(duration,function()
    {
        if($(e).next().length == 0)
        {
            if(typeof(callback) == 'function')
            {
                callback();
            }
            return;
        }
        else
        {
            // Apply recursion for every sibling.
            fade_in_recursive($(e).next(),duration,callback);
        }
    });

} // End function

$('#click_me').click(function(){
    fade_in_recursive($('#test'), 1000, function(){alert('all done');});
});

Example: http://jsfiddle.net/AlienWebguy/9npvz/2/


You may try this

    var flag = false;
    $( '#clickme' ).click( function() {
    $("#test").fadeIn(function(){
      $("#testone").show().fadeIn("3000", function(){
        $("#testtwo").fadeIn("4500", function(){
          $("#testthree").fadeIn("6000", function(){
        $("#testfour").fadeIn("7500", function(){
          $("#testfive").fadeIn("9000", function(){
            flag = true;
            afterClick(flag);
            return false;
              });
            });
          });
        });
      });   
    });
});

function afterClick(flag){
$(document).click( function() {
  if(flag == true){
    $( '#test' ).hide(1000);
    return false; 
  }
  flag = false;
});
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号