使用 jquery 在 x 秒后淡出 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13425347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:36:40  来源:igfitidea点击:

fade out div after x seconds with jquery

jquerytimerdelay

提问by michele

I do a fade in div that is not displayed when I load the page:

我在加载页面时淡入 div 不显示:

    $('#overlay').fadeIn('fast');
    $('#box').fadeIn('slow');

I would do this instructions after x seconds, doing a fadeOut of the div:

我会在 x 秒后执行此指令,对 div 执行淡出:

$('#overlay').fadeOut('fast');
$('#box').hide();

How can I do it? Actually fadeOut is done on button click.

我该怎么做?实际上,fadeOut 是在单击按钮时完成的。

The script is here: http://clouderize.it/cookie-localstorage/a.phpThe div that appear when I click on another image will disappear after x seconds. Thanks a lot.

脚本在这里:http://clouderize.it/cookie-localstorage/a.php 当我单击另一个图像时出现的 div 将在 x 秒后消失。非常感谢。

回答by Asad Saeeduddin

The .delaymethod is purpose built for what you are describing:

.delay方法专为您所描述的内容而构建:

$('#overlay').fadeIn('fast').delay(1000).fadeOut('fast');
$('#box').fadeIn('slow').delay(1000).hide(0);

http://jsfiddle.net/SUBnz/1/

http://jsfiddle.net/SUBnz/1/

回答by Gabe

You could use setTimeout()

你可以用 setTimeout()

var xSeconds = 1000; // 1 second

setTimeout(function() {
   $('#overlay').fadeOut('fast');
   $('#box').hide();
}, xSeconds);

回答by Dharti Gohil

Maybe this is too late to reply but I found a way which helped me.

也许现在回复为时已晚,但我找到了一种帮助我的方法。

$("#overlay").fadeTo(10000,1).fadeOut(5000);

$("#overlay").fadeTo(10000,1).fadeOut(5000);

Refer this link http://juristr.com/blog/2009/12/howto-fade-out-div-after-some-seconds/

请参阅此链接 http://juristr.com/blog/2009/12/howto-fade-out-div-after-some-seconds/

It allows you to set a time like when you want the div to disappear and with what speed.

它允许您设置一个时间,例如您希望 div 消失的时间以及速度。