jQuery 在 showDiv() setTimeout 之后淡入 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7153369/
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
fade in div following showDiv() setTimeout
提问by JD2011
Really quick question - (I hope!)
非常快的问题-(我希望!)
I have a div that doesnt display until the rest of the page has loaded using:
我有一个 div 在页面的其余部分加载之前不会显示:
<script>
function showDiv(){
var obj = document.getElementById('test');
obj.style.display = 'block';
}
</script>
in the head of my html, and the following code in the body:
在我的 html 的头部,以及正文中的以下代码:
<body onLoad="setTimeout('setTimeout(showDiv()', 1000);">
<div id="test" style="display:none;">
While the div loads after a set time correctly, I was wondering if there was a way to fade this div in after the given "Timeout", using something like:
虽然 div 在设定的时间后正确加载,但我想知道是否有办法在给定的“超时”之后淡入这个 div,使用类似的方法:
<script type="text/javascript">
$(function(){ // $(document).ready shorthand
$('#test').hide().fadeIn(1500);
});
</script>
for example? At the moment, the div suddenly appears, although I am hoping to get it to fade in somehow. I have tried to merge these two js functions, but to this end unsuccessfully. Any help or advice much appreciated!
例如?目前,div 突然出现,虽然我希望它以某种方式淡入。我曾尝试合并这两个js函数,但未成功。非常感谢任何帮助或建议!
回答by AlienWebguy
$(function(){
$('#test').hide();
setTimeout(function(){
$('#test').fadeIn('slow');
},1000);
});
回答by qwertymk
$('#test').hide().delay(1500).fadeIn('slow');
$('#test').hide().delay(1500).fadeIn('slow');
JsFiddle DEMO
JsFiddle 演示
回答by demux
You should use the load event in jquery, like so:
您应该在 jquery 中使用 load 事件,如下所示:
$(window).load(function() {
$('#test').fadeIn('slow');
});
See: http://api.jquery.com/load-event/
见:http: //api.jquery.com/load-event/
<body onLoad="setTimeout('setTimeout(showDiv()', 1000);">
is not cool in jquery land. :p
<body onLoad="setTimeout('setTimeout(showDiv()', 1000);">
在 jquery 领域并不酷。:p
See also: http://api.jquery.com/ready/