fade out div after x seconds with jquery

空扰寡人 提交于 2019-12-20 11:28:08

问题


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

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

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

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

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

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


回答1:


The .delay method is purpose built for what you are describing:

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

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




回答2:


You could use setTimeout()

var xSeconds = 1000; // 1 second

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



回答3:


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

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

Refer this link 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.



来源:https://stackoverflow.com/questions/13425347/fade-out-div-after-x-seconds-with-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!