How do you pause before fading an element out using jQuery?

后端 未结 8 465
滥情空心
滥情空心 2020-12-29 03:12

I would like to flash a success message on my page.

I am using the jQuery fadeOut method to fade and then remove the element. I can increase the duratio

相关标签:
8条回答
  • 2020-12-29 03:55

    dansays's answer just doesn't work for me. For some reason, remove() runs immediately and the div disappears before any animations happen.

    The following works, however (by omitting the remove() method):

    $('#foo').fadeIn(500).delay(5000).fadeOut(500);
    

    I don't mind if there are hidden DIVs on the page (there shouldn't be more than a few anyway).

    0 讨论(0)
  • 2020-12-29 03:57

    While @John Sheehan's approach works, you run into the jQuery fadeIn/fadeOut ClearType glitch in IE7. Personally, I'd opt for @John Millikin's setTimeout() approach, but if you're set on a pure jQuery approach, better to trigger an animation on a non-opacity property, such as a margin.

    var left = parseInt($('#element').css('marginLeft'));
    $('#element')
        .animate({ marginLeft: left ? left : 0 }, 5000)
        .fadeOut('fast');
    

    You can be a bit cleaner if you know your margin to be a fixed value:

    $('#element')
        .animate({ marginLeft: 0 }, 5000)
        .fadeOut('fast');
    

    EDIT: It looks like the jQuery FxQueues plug-in does just what you need:

    $('#element').fadeOut({
        speed: 'fast',
        preDelay: 5000
    });
    
    0 讨论(0)
  • 2020-12-29 04:06

    Following on from dansays' comment, the following seems to work perfectly well:

    $('#thing') .animate({dummy:1}, 2000) .animate({ etc ... });

    0 讨论(0)
  • 2020-12-29 04:09

    Update for 1.6.2

    Nathan Long's answer will cause the element to pop off without obeying delay or fadeOut.

    This works:

    $('#foo').delay(2000).fadeOut(2000);
    
    0 讨论(0)
  • 2020-12-29 04:10

    The new delay() function in jQuery 1.4 should do the trick.

    $('#foo').fadeIn(200).delay(5000).fadeOut(200).remove();
    
    0 讨论(0)
  • 2020-12-29 04:13

    use setTimeout(function(){$elem.hide();}, 5000);

    Where $elem is the element you wish to hide, and 5000 is the delay in milliseconds. You can actually use any function within the call to setTimeout(), that code just defines a small anonymous function for simplicity.

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