Javascript 页面加载 2 秒后显示 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14636362/
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-24 17:26:39  来源:igfitidea点击:

Show div after 2 seconds of page load

javascriptjquery

提问by CodingWonders90

I am currently trying to show a div 2 seconds after the page is loaded. I can successfully do the reverse by hiding the div two seconds after the page loads. The issue is that nothing occurs and the div stays hidden. How can I properly show a div after two seconds of page load? Extra: mean while the two seconds are running show an ajax loading gif and then fade in the div

我目前正在尝试在页面加载后 2 秒显示一个 div。我可以通过在页面加载后两秒隐藏 div 来成功地执行相反的操作。问题是什么也没有发生,div 保持隐藏状态。如何在页面加载两秒后正确显示 div?额外:意思是在两秒钟运行时显示 ajax 加载 gif 然后淡入 div

<script type = "text/javascript">  
$(window).load(function() {
    setTimeout(function() {
        $("#contentPost").show('fadeIn', {}, 500)
    }, 2000);
});
</script>

html/css

html/css

<style>
.contentPost { display:none;}
</style>

<div class="contentPost">
 <h2>Hi there</h2>
</div>

回答by Mooseman

$(document).ready(function() {
    $(".contentPost").delay(2000).fadeIn(500);
});

Will work perfectly.

将完美运行。

回答by Undefined

Ive never seen your show method written like that. Try altering it into use the jquery method fadeIn:

我从来没有见过你这样写的 show 方法。尝试将其更改为使用 jquery 方法fadeIn

<script>
$(function() {
  $("#contentPost").delay(2000).fadeIn(500);
});
</script>

The showmethod does not accept any arguments and won't work as you want it to.

显示方法不接受任何参数,当你希望它不会起作用。