tags:

views:

41

answers:

1

I have a div that has content in it.

<section id="latest-news">
  <p>Pellentesque habitant morbi
  tristique senectus et netus
  et malesuada fames ac turpis
  egestas.</p>
</section>

But while the page is loading, I want a loading .gif to appear, wait about 5 seconds and the the text. With a fadeIn or slide down animation too. (The text would be hidden by default)

I tried for an hour on this but I can't get it to work :/

What is a way to do this?

+2  A: 
<div id='loading'><img src='loading.gif' /></div>
<div id='content' style='display:none'><p>content here</p></div>

<script type='text/javascript'>
$(document).ready(function(){
  setTimeout(function(){
    $('#loading').hide(); 
    $('#content').fadeIn();
    }
  , 5000);
}
</script>

I'm not sure if you can put anonymous function inside setTimeout, but that's the general idea.

Jasmo
This might be a problem is javascript is disabled ... as the content would remain hidden ... I think it would be better to call `hide()` on the content as soon as possible (i.e. just after the content, for instance), and make it appear later ... (on `ready` for instance)
tsimbalar
Yes, that's true - but in that case the content would be visible before onReady is run. I don't know if the author is creating this loading just to make customers wait, or what :)
Jasmo
Yes, that's true, but better seeing the content a bit too early than never :DAnother way is to include some CSS only is javascript is enabled, that will contain display info just for that. It is described in the "jQuery Cookbook" with that snippet :`<script type="text/javascript">/* <![CDATA[ */document.write('<link rel="stylesheet" type="text/css" href="preload.css" />');/* ]]> */</script>`this way, if you have javascript, you will not see the content "appear and disappear"but the question could be more explicit :P
tsimbalar