Javascript、jQuery、Do 函数一个又一个函数
我有这个 JavaScript 函数。
<script type="text/javascript">
$(document).ready(function () {
$('#load').html('<img style="display:block; margin:10px auto;background-color: #EEE;" src="<?echo $site["url"];?>images/icons/loading.gif"/>');
$('#loaded').fadeIn(1500);
$('#load').fadeOut(2000);
});
</script>
正如您所看到的,它只是运行加载图像大约几秒钟,然后显示 div 并隐藏加载图像。 这对我来说不好。我需要一个能以这种方式工作的函数。代码的逻辑是:
As soon as $('#load').html('<img style="display:block; margin:10px auto;background-color: #EEE;" src="<?echo $site["url"];?>images/icons/loading.gif"/>'); is running, then wait 4 seconds, and then show the #loaded DIV.
通过这种方式,#loaded DIV 将被完全加载,并且它会正常工作,因为它是一个重的 DIV。
简而言之,我必须确保#loaded 在显示之前已被浏览器完全加载。 我怎样才能创建一个函数来做到这一点?
文档准备好后显示加载 DIV。然后等待3-4秒,然后使加载消失并且加载的DIV可见。
这可能吗?
I have this javascript function.
<script type="text/javascript">
$(document).ready(function () {
$('#load').html('<img style="display:block; margin:10px auto;background-color: #EEE;" src="<?echo $site["url"];?>images/icons/loading.gif"/>');
$('#loaded').fadeIn(1500);
$('#load').fadeOut(2000);
});
</script>
As you may see it simply runs a loading image for a tot of seconds, then shows the div and hide the loading image.
This is not good for me. I need a function that will work in this way. The logic of the code would be:
As soon as $('#load').html('<img style="display:block; margin:10px auto;background-color: #EEE;" src="<?echo $site["url"];?>images/icons/loading.gif"/>'); is running, then wait 4 seconds, and then show the #loaded DIV.
In this way the #loaded DIV will be fully loaded and it will work fine, since it is an heavy one.
In few words I must to be sure that #loaded has been fully loaded by the browser before showing it.
How can I create a function that will do that?
On document ready show the loading DIV. Then WAIT for 3-4 seconds, then make the loading disappear and the loaded DIV visible.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让淡入启动作为淡出的回调。
但为了正确地做到这一点,我会将 #load div 添加到 html 中,通过 css 隐藏它。然后只需在 document.ready 上执行 fadeOut 即可。
更新。
结合安德鲁和我的答案的优点,这应该可以完美地工作:
make your fadein launch as a callback of your fadeout.
But to do this properly i would add the #load div to the html, hide it via css. Then simply do the fadeOut on document.ready.
update.
Mixing the best of both Andrew and my answer, this should work flawlessly:
您的代码不会“加载”图像。它所做的只是将图像标签添加到 DOM 中。将标签添加到 DOM 后,浏览器将尝试添加图像。
最好的办法是将图像添加到 HTML 代码中并使用 css 隐藏它。然后使用 jQuery 的 show 函数来显示它,而不是尝试加载图像。
另一种方法是使用 JavaScript“预加载”图像,但我不知道这会如何更有效。
Your code won't "load" the image. All it does is add the image tag to the DOM. The browser will try to add the image once the tag is added to the DOM.
Your best bet is to add the image to the HTML code and hide it with css. Then use jQuery's show function to show it rather than try to load the image.
An alternative would be to "preload" the image with javascript, but I don't see how that would be more efficient.
您可以将处理程序绑定到 #loaded div 上的
load
事件:请参阅 http ://api.jquery.com/load-event/
You could bind a handler to the
load
event on the #loaded div:See http://api.jquery.com/load-event/
我对所有这些答案有点困惑,不确定你的问题是什么,或者我是对的还是他们是对的,但这就是如何等待 4 秒钟。 (在原始代码中添加了 2 行)
I am a little confused by all these answers and not sure what your question is or if I am right or they are right, but this is how to wait 4 seconds. (added 2 lines to your original code)