简体   繁体   English

在使用jQuery淡化元素之前,你如何暂停?

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

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. 我使用jQuery fadeOut方法淡化然后删除元素。 I can increase the duration to make it last longer, however this looks strange. 我可以增加持续时间以使其持续更长时间,但这看起来很奇怪。

What I would like to happen is have the element be displayed for five seconds, then fade quickly, and finally be removed. 我想要发生的是让元素显示五秒钟,然后快速淡化,最后被删除。

How can you animate this using jQuery? 你怎么能使用jQuery动画这个?

jQuery 1.4中的新delay()函数应该可以解决问题。

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

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

Where $elem is the element you wish to hide, and 5000 is the delay in milliseconds. 其中$elem是您要隐藏的元素, 5000是延迟(以毫秒为单位)。 You can actually use any function within the call to setTimeout() , that code just defines a small anonymous function for simplicity. 实际上,您可以在调用setTimeout()使用任何函数,该代码只是为了简单起见而定义了一个小的匿名函数。

While @John Sheehan's approach works, you run into the jQuery fadeIn/fadeOut ClearType glitch in IE7. 虽然@John Sheehan的方法有效,但是你会遇到IE7中的jQuery fadeIn / fadeOut ClearType故障 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. 就个人而言,我选择了@John Millikin的setTimeout()方法,但如果你采用纯jQuery方法,最好在非不透明属性上触发动画,例如边距。

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: 编辑 :看起来jQuery FxQueues插件可以满足您的需求:

$('#element').fadeOut({
    speed: 'fast',
    preDelay: 5000
});

For a pure jQuery approach, you can do 对于纯jQuery方法,您可以这样做

$("#element").animate({opacity: 1.0}, 5000).fadeOut();

It's a hack, but it does the job 这是一个黑客,但它完成了这项工作

var $msg = $('#msg-container-id');
$msg.fadeIn(function(){
  setTimeout(function(){
    $msg.fadeOut(function(){
      $msg.remove();
    });
  },5000);
});

根据dansays的评论,以下似乎运作良好:

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

dansays's answer just doesn't work for me. dansays的答案对我不起作用。 For some reason, remove() runs immediately and the div disappears before any animations happen. 出于某种原因, remove()立即运行,div在任何动画发生之前消失。

The following works, however (by omitting the remove() method): 但是,以下工作(通过省略remove()方法):

$('#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). 我不介意页面上是否有隐藏的DIV(不管怎样,不应该有多个DIV)。

Update for 1.6.2 1.6.2的更新

Nathan Long's answer will cause the element to pop off without obeying delay or fadeOut . Nathan Long的回答将导致元素弹出而不遵守延迟或fadeOut

This works: 这有效:

$('#foo').delay(2000).fadeOut(2000);

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

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