admin管理员组文章数量:1432222
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?
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?
Share Improve this question edited May 16, 2011 at 23:38 OMG Ponies 333k85 gold badges535 silver badges508 bronze badges asked May 16, 2011 at 23:30 DiegoP.DiegoP. 45.8k34 gold badges90 silver badges110 bronze badges4 Answers
Reset to default 3make your fadein launch as a callback of your fadeout.
$(document).ready(function() {
$('#load')
.html('<img style="display:block; margin:10px auto;background-color: #EEE;" src="<?echo $site["url"];?>images/icons/loading.gif"/>')
.fadeOut(2000, function() {
$('#loaded').fadeIn(1500);
});
});
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:
<div id="load" style="display:none">Loading, please wait</div>
$(document).ready(function() {
$('#load').show();
$('#loaded').load(function() {
$('#load').fadeOut(500,function(){ $(this).fadeIn(500);});
});
});
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.
You could bind a handler to the load
event on the #loaded div:
$(document).ready(function() {
$('#loaded').load(function() {
$('#loaded').fadeIn(500);
$('#load').fadeOut(500);
});
});
See http://api.jquery./load-event/
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)
<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"/>');
setTimeout(function () {
$('#loaded').fadeIn(1500);
$('#load').fadeOut(2000);
}, 4000)
});
</script>
本文标签: javascriptjQueryDo function after another functionStack Overflow
版权声明:本文标题:Javascript, jQuery, Do function after another function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745221280a2648406.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论