几秒后隐藏div

发布于 2024-07-19 06:58:22 字数 77 浏览 3 评论 0原文

我想知道,在jquery中我如何能够在几秒钟后隐藏div? 例如 Gmail 的消息。

我已尽力但无法使其正常工作。

I was wondering, how in jquery am I able to hide a div after a few seconds? Like Gmail's messages for example.

I've tried my best but am unable to get it working.

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

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

发布评论

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

评论(9

莫言歌 2024-07-26 06:58:22

这将在 1 秒(1000 毫秒)后隐藏 div。

setTimeout(function() {
    $('#mydiv').fadeOut('fast');
}, 1000); // <-- time in milliseconds
#mydiv{
    width: 100px;
    height: 100px;
    background: #000;
    color: #fff;
    text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mydiv">myDiv</div>

如果您只想隐藏而不褪色,请使用hide()

This will hide the div after 1 second (1000 milliseconds).

setTimeout(function() {
    $('#mydiv').fadeOut('fast');
}, 1000); // <-- time in milliseconds
#mydiv{
    width: 100px;
    height: 100px;
    background: #000;
    color: #fff;
    text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mydiv">myDiv</div>

If you just want to hide without fading, use hide().

夏の忆 2024-07-26 06:58:22

您可以尝试 .delay()

$(".formSentMsg").delay(3200).fadeOut(300);

调用 div 设置延迟时间(以毫秒为单位)并设置您想要更改的属性,在本例中我使用 .fadeOut() 因此它可以是动画的,但是您也可以使用 .hide() 。

http://api.jquery.com/delay/

You can try the .delay()

$(".formSentMsg").delay(3200).fadeOut(300);

call the div set the delay time in milliseconds and set the property you want to change, in this case I used .fadeOut() so it could be animated, but you can use .hide() as well.

http://api.jquery.com/delay/

你对谁都笑 2024-07-26 06:58:22

jquery 提供了多种方法以定时方式隐藏 div,不需要设置和稍后清除或重置间隔计时器或其他事件处理程序。 这里有一些例子。

纯隐藏,一秒延迟

// hide in one second
$('#mydiv').delay(1000).hide(0); 

纯隐藏,无延迟

// hide immediately
$('#mydiv').delay(0).hide(0); 

动画隐藏

// start hide in one second, take 1/2 second for animated hide effect
$('#mydiv').delay(1000).hide(500); 

淡出

// start fade out in one second, take 300ms to fade
$('#mydiv').delay(1000).fadeOut(300); 

此外,这些方法可以将队列名称或函数作为第二个参数(取决于方法)。 上述所有调用和其他相关调用的文档可以在此处找到:
https://api.jquery.com/category/effects/

jquery offers a variety of methods to hide the div in a timed manner that do not require setting up and later clearing or resetting interval timers or other event handlers. Here are a few examples.

Pure hide, one second delay

// hide in one second
$('#mydiv').delay(1000).hide(0); 

Pure hide, no delay

// hide immediately
$('#mydiv').delay(0).hide(0); 

Animated hide

// start hide in one second, take 1/2 second for animated hide effect
$('#mydiv').delay(1000).hide(500); 

fade out

// start fade out in one second, take 300ms to fade
$('#mydiv').delay(1000).fadeOut(300); 

Additionally, the methods can take a queue name or function as a second parameter (depending on method). Documentation for all the calls above and other related calls can be found here:
https://api.jquery.com/category/effects/

披肩女神 2024-07-26 06:58:22

有一个非常简单的方法来做到这一点。

问题是 .delay 只影响动画,所以你需要做的是通过给它一个持续时间来使 .hide() 表现得像一个动画。

$("#whatever").delay().hide(1);

通过给它一个较短的持续时间,它看起来就像常规的 .hide 函数一样是即时的。

There's a really simple way to do this.

The problem is that .delay only effects animations, so what you need to do is make .hide() act like an animation by giving it a duration.

$("#whatever").delay().hide(1);

By giving it a nice short duration, it appears to be instant just like the regular .hide function.

战皆罪 2024-07-26 06:58:22
$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}

来自 http://james.padolsey.com/javascript/jquery-delay-plugin/< /a>

(允许方法链接)

$.fn.delay = function(time, callback){
    // Empty function:
    jQuery.fx.step.delay = function(){};
    // Return meaningless animation, (will be added to queue)
    return this.animate({delay:1}, time, callback);
}

From http://james.padolsey.com/javascript/jquery-delay-plugin/

(Allows chaining of methods)

疯了 2024-07-26 06:58:22

使用 jQuery 计时器还允许您拥有与附加到对象的计时器相关联的名称。 因此,您可以将多个计时器附加到一个对象并停止其中任何一个。

$("#myid").oneTime(1000, "mytimer1" function() {
  $("#something").hide();
}).oneTime(2000, "mytimer2" function() {
  $("#somethingelse").show();  
});

$("#myid").stopTime("mytimer2");

eval 函数(及其相关函数 Function、setTimeout 和 setInterval)提供对 JavaScript 编译器的访问。 有时这是必要的,但在大多数情况下,它表明存在极其糟糕的编码。 eval 函数是 JavaScript 中最常被滥用的功能。

http://www.jslint.com/lint.html

Using the jQuery timer will also allow you to have a name associated with the timers that are attached to the object. So you could attach several timers to an object and stop any one of them.

$("#myid").oneTime(1000, "mytimer1" function() {
  $("#something").hide();
}).oneTime(2000, "mytimer2" function() {
  $("#somethingelse").show();  
});

$("#myid").stopTime("mytimer2");

The eval function (and its relatives, Function, setTimeout, and setInterval) provide access to the JavaScript compiler. This is sometimes necessary, but in most cases it indicates the presence of extremely bad coding. The eval function is the most misused feature of JavaScript.

http://www.jslint.com/lint.html

咿呀咿呀哟 2024-07-26 06:58:22

也许最简单的方法是使用计时器插件。 http://plugins.jquery.com/project/timers 然后调用类似的内容

$(this).oneTime(1000, function() {
    $("#something").hide();
  });

Probably the easiest way is to use the timers plugin. http://plugins.jquery.com/project/timers and then call something like

$(this).oneTime(1000, function() {
    $("#something").hide();
  });
若能看破又如何 2024-07-26 06:58:22
<script>
      $(function() {
      $(".hide-it").hide(7000);
    });              
</script>

<div id="hide-it">myDiv</div>
<script>
      $(function() {
      $(".hide-it").hide(7000);
    });              
</script>

<div id="hide-it">myDiv</div>
毅然前行 2024-07-26 06:58:22

我们可以直接使用

$('#selector').delay(5000).fadeOut('slow');

we can directly use

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