超时 jQuery 效果

发布于 2024-07-10 00:34:46 字数 168 浏览 3 评论 0原文

我试图让一个元素淡入,然后在 5000 毫秒内再次淡出。 我知道我可以做类似的事情:

setTimeout(function () { $(".notice").fadeOut(); }, 5000);

但这只会控制淡出,我会在回调中添加上述内容吗?

I am trying to have an element fade in, then in 5000 ms fade back out again. I know I can do something like:

setTimeout(function () { $(".notice").fadeOut(); }, 5000);

But that will only control the fade out, would I add the above on the callback?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

过潦 2024-07-17 00:34:46

更新:从 jQuery 1.4 开始,您可以使用 .delay( n ) 方法。 http://api.jquery.com/delay/

$('.notice').fadeIn().delay(2000).fadeOut('slow'); 

注意:< code>$.show() 和 $.hide() 默认情况下不会排队,因此如果您想将 $.delay() 与它们一起使用,你需要这样配置它们:

$('.notice')
    .show({duration: 0, queue: true})
    .delay(2000)
    .hide({duration: 0, queue: true});

你可以使用 Queue 语法,这可能有效:

jQuery(function($){ 

var e = $('.notice'); 
e.fadeIn(); 
e.queue(function(){ 
  setTimeout(function(){ 
    e.dequeue(); 
  }, 2000 ); 
}); 
e.fadeOut('fast'); 

}); 

或者你可以非常巧妙地创建一个 jQuery 函数来完成它。

(function($){ 

  jQuery.fn.idle = function(time)
  { 
      var o = $(this); 
      o.queue(function()
      { 
         setTimeout(function()
         { 
            o.dequeue(); 
         }, time);
      });
  };
})(jQuery);

这将(理论上,在这里处理内存)允许你这样做:

$('.notice').fadeIn().idle(2000).fadeOut('slow'); 

Update: As of jQuery 1.4 you can use the .delay( n ) method. http://api.jquery.com/delay/

$('.notice').fadeIn().delay(2000).fadeOut('slow'); 

Note: $.show() and $.hide() by default are not queued, so if you want to use $.delay() with them, you need to configure them that way:

$('.notice')
    .show({duration: 0, queue: true})
    .delay(2000)
    .hide({duration: 0, queue: true});

You could possibly use the Queue syntax, this might work:

jQuery(function($){ 

var e = $('.notice'); 
e.fadeIn(); 
e.queue(function(){ 
  setTimeout(function(){ 
    e.dequeue(); 
  }, 2000 ); 
}); 
e.fadeOut('fast'); 

}); 

or you could be really ingenious and make a jQuery function to do it.

(function($){ 

  jQuery.fn.idle = function(time)
  { 
      var o = $(this); 
      o.queue(function()
      { 
         setTimeout(function()
         { 
            o.dequeue(); 
         }, time);
      });
  };
})(jQuery);

which would ( in theory , working on memory here ) permit you do to this:

$('.notice').fadeIn().idle(2000).fadeOut('slow'); 
站稳脚跟 2024-07-17 00:34:46

我刚刚想通了:

$(".notice")
   .fadeIn( function() 
   {
      setTimeout( function()
      {
         $(".notice").fadeOut("fast");
      }, 2000);
   });

我会为其他用户保留该帖子!

I just figured it out below:

$(".notice")
   .fadeIn( function() 
   {
      setTimeout( function()
      {
         $(".notice").fadeOut("fast");
      }, 2000);
   });

I will keep the post for other users!

摘星┃星的人 2024-07-17 00:34:46

@strager 的伟大黑客。 像这样将其实现到 jQuery 中:

jQuery.fn.wait = function (MiliSeconds) {
    $(this).animate({ opacity: '+=0' }, MiliSeconds);
    return this;
}

然后将其用作:

$('.notice').fadeIn().wait(2000).fadeOut('slow');

Great hack by @strager. Implement it into jQuery like this:

jQuery.fn.wait = function (MiliSeconds) {
    $(this).animate({ opacity: '+=0' }, MiliSeconds);
    return this;
}

And then use it as:

$('.notice').fadeIn().wait(2000).fadeOut('slow');
一身软味 2024-07-17 00:34:46

你可以做这样的事情:

$('.notice')
    .fadeIn()
    .animate({opacity: '+=0'}, 2000)   // Does nothing for 2000ms
    .fadeOut('fast');

遗憾的是,你不能只做 .animate({}, 2000) ——我认为这是一个错误,并且会报告它。

You can do something like this:

$('.notice')
    .fadeIn()
    .animate({opacity: '+=0'}, 2000)   // Does nothing for 2000ms
    .fadeOut('fast');

Sadly, you can't just do .animate({}, 2000) -- I think this is a bug, and will report it.

合久必婚 2024-07-17 00:34:46

Ben Alman 为 jQuery 编写了一个名为 doTimeout 的实用插件。 它有很多不错的功能!

在这里查看:jQuery doTimeout:类似于 setTimeout,但更好

Ben Alman wrote a sweet plugin for jQuery called doTimeout. It has a lot of nice features!

Check it out here: jQuery doTimeout: Like setTimeout, but better.

万劫不复 2024-07-17 00:34:46

为了能够像这样使用它,您需要返回 this。 如果没有返回,fadeOut('slow') 将无法获得执行该操作的对象。

即:

  $.fn.idle = function(time)
  {
      var o = $(this);
      o.queue(function()
      {
         setTimeout(function()
         {
            o.dequeue();
         }, time);
      });
      return this;              //****
  }

然后这样做:

$('.notice').fadeIn().idle(2000).fadeOut('slow');

To be able to use it like that, you need to return this. Without the return, fadeOut('slow'), will not get an object to perform that operation on.

I.e.:

  $.fn.idle = function(time)
  {
      var o = $(this);
      o.queue(function()
      {
         setTimeout(function()
         {
            o.dequeue();
         }, time);
      });
      return this;              //****
  }

Then do this:

$('.notice').fadeIn().idle(2000).fadeOut('slow');
放手` 2024-07-17 00:34:46

只需几行 jQuery 即可完成此操作:

$(function(){
    // make sure img is hidden - fade in
    $('img').hide().fadeIn(2000);

    // after 5 second timeout - fade out
    setTimeout(function(){$('img').fadeOut(2000);}, 5000);
});​

请参阅下面的小提琴以获取工作示例...

http:// /jsfiddle.net/eNxuJ/78/

This can be done with only a few lines of jQuery:

$(function(){
    // make sure img is hidden - fade in
    $('img').hide().fadeIn(2000);

    // after 5 second timeout - fade out
    setTimeout(function(){$('img').fadeOut(2000);}, 5000);
});​

see the fiddle below for a working example...

http://jsfiddle.net/eNxuJ/78/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文