views:

80

answers:

5

I'm brand new to jQuery (and javascript in general). I've been meaning to use it/learn it for some time, and nows the time.

So I'm going to use jQuery on this page to fade out the "under construction" warning at the top of the page on load. But I'm unfamiliar with any of the functions or selectors to use.

I found this basic article at jQuery which has a demo that seems to work perfect for what I'm doing, only it's set up to fade out on click.

What do I need to change to get it to fade out after a few seconds once the page has loaded, and can you point me where to look to find out more about those type of functions (specifically on jQuery's site, if you're familiar). And please don't say, "In the documentation." I'm a noob but not a dummy, thanks. I'm just trying to learn more about that particular area of the syntax responsible for functions for now (if that's what they're called).

+3  A: 
$(document).ready(function() {
   window.setTimeout("fadeMyDiv();", 3000); //call fade in 3 seconds
 }
)

function fadeMyDiv() {
   $("#myDiv").fadeOut('slow');
}
JohnFx
Couldn't get it to work - and not sure which parts of this to replace with my actual id. Just this piece?$("#tempwarning")
JAG2007
Yes, if tempwarning is the ID attribute of the div you want to fade.
JohnFx
OK, working now. I think I had an error in my link to the jQuery library.
JAG2007
BTW, how do I know where to place my scripts? It seems like some belong in the head, and some right before the ending body tag?
JAG2007
Scripts load one at a time. Since you're using the $(document).ready, then you can put your scripts at the end (in this case). Just make sure that the jquery library is placed before your function. A good rule of thumb is to actually try and get as much of your scripts at the bottom as possible.
rockinthesixstring
+1  A: 

Hey, the site looks cool. This should do it for you.

$(document).ready(function() {
    setTimeout(fade, 2000);
}

function fade(){
   $("#tempwarning").fadeOut(1000);
}
Derrick
Isn't working. It's there now, but not doing anything.
JAG2007
Ok, I think I might have had an error in my link to the jQuery library. Thanks for your answer.
JAG2007
No problem. You may want to check into firebug, or firebug lite. ;)
Derrick
I might also suggest that you use a specific version from the Google API CDN : http://code.google.com/apis/libraries/devguide.html#jquery
Derrick
A: 
$(document).ready(function(){
    setTimeout(function(){ $("#tempwarning").fadeOut(); }, 2000);
});
You
This won't pause for a few seconds before fading out as specified by the OP
JohnFx
So much for only reading the title.
You
That's why there is an edit button. =)
JohnFx
+2  A: 

Here's a complete example.

Just paste this into an html file and save it.

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.4.2.min.js" />
    <script>
    $(document).ready(function(){   
        window.setTimeout('fadeout();', 3000);
    });

    function fadeout(){
        $('#tempwarning').fadeOut('slow', function() {
           // Animation complete.
        });
    }

    </script>
</head>
<body>
    <div id="tempwarning">

        <div class="wrap">

            <span class="warning-icon">!</span>

            <p>
                Oops! Please pardon my appearance. I'm still <a href="http://graphicriver.net/item/under-construction-graphic/53758?ref=jglovier"&gt;under development</a>. Please visit often.
            </p>

        </div>

    </div>
</body>
</html>

EDIT:

This line of code:

 $(document).ready(function() { }); 

Waits for the page to load before executing anything inside it.

Brandon Boone
You still need code that waits a few seconds before fading.
JohnFx
Very true, I missed that part in the question. Great answer by the way!
Brandon Boone
+4  A: 

New to jQuery 1.4 is the delay method. Using it for this purpose:

$("#myDiv").delay(3000).fadeOut("slow");
cnanney
Awesome, taught me something new!
Brandon Boone
delay is great for this :)
Alastair Pitts