Jquery Fade In Site/content Once It's Loaded?
Solution 1:
First, a side point: In general, web designers spend a lot of time trying to improve perceived page load time, getting things shown as quickly as possible. This actively goes the other way, presenting a blank page until everything is ready, which may not be ideal.
But if it's appropriate for your situation:
Since everything visible will be a descendant of body
, you could load body
hidden and then fade it in. It would be important to include a fallback for users without JavaScript (typically fewer than 2% at least according to Yahoo, but still). So along the lines of:
<!DOCTYPE html><html><head><title>Example</title><!-- This next would probably be in your main CSS file, but shown
inline here just for purposes of the example
--><styletype="text/css">body {
/* Hide it for nifty fade-in effect */display: none;
}
</style><!-- Fallback for non-JavaScript people --><noscript><styletype="text/css">body {
/* Re-displays it by overriding the above */display: block;
}
</style></noscript></head><body>
...content...
<scriptsrc="jquery.js"></script><script>// This version runs the function *immediately*
(function($) {
$(document.body).fadeIn(1000);
})(jQuery);
</script></body></html>
A couple of variations on the script part of that, depending on when you want the fade-in to occur:
Wait for "ready" event:
// This version waits until jQuery's "ready" eventjQuery(function($) {
$(document.body).fadeIn(1000);
});
Wait for the window#load
event:
// This version waits until the window#load event
(function($) {
$(window).load(function() {
$(document.body).fadeIn(1000);
});
})(jQuery);
window#load
fires very late in the page load process, after all external resources (including all images) have loaded. But you said you wanted to wait until everything was loaded, so that might be what you want to do. It will definitely make your page seem slower...
Solution 2:
Yes, wrap your content with a div (e.g. <div id="main-content">
) and in your css put this:
div#main-content { display: none; }
Then use this script.
$(document).ready(function() {
$('#main-content').fadeIn();
});
Solution 3:
Another way to accomplish this is via the Animate.css project. Its pure CSS so there is no need to rely on JS:
Just add the 'animated' and 'fadeIn' class to your parent element and all children will fade in.
Solution 4:
jQuery(document).ready(function(){
jQuery("#container").fadeIn();
});
where container is the id of the div containing your entire content.
Solution 5:
$(document).ready(function() {
$('body').hide().fadeIn('fast');
});
Post a Comment for "Jquery Fade In Site/content Once It's Loaded?"