Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Javascript for fading in page when FULLY loaded?

Featured Replies

Hi guys,

 

Just playing around with a site based on a 6-column grid layout. What I'd like to do is to have the site quickly fade in (from black) after ALL the elements have completely loaded. At the moment, there's a twitter feed in place that seems to take the longest to load but I can't seem to come up with any javascript that will only kick in after the twitter api has been accessed and read.

 

I'm using a fair bit of jquery and I was thinking the code might look a little bit like this:

 

$(window).ready(function() {
   $('body').css({'display' : 'none'}).fadeIn(3000);
};

 

Or maybe:

 

$(document).ready(function() {
   $('body').hide().fadeIn(5000);
});

 

The problem with those is that they don't wait until the twitter api has been accessed and loaded into the page. Anyone got any ideas on how I might be able to do it?? Maybe it's not possible!

Have you tried putting it onto a timer? You could put it onto 5 seconds (for example) then let it fade in?

 

Have the body as display:none, and when it fades in, it is displayed?

 

Just a suggestion, as im not sure how it would check everything has loaded

  • Author

Have you tried putting it onto a timer? You could put it onto 5 seconds (for example) then let it fade in?

 

Have the body as display:none, and when it fades in, it is displayed?

 

Just a suggestion, as im not sure how it would check everything has loaded

 

Yeah, I've pretty much tried that but i still doesn't wait until the twitter api has done its thing.

 

I thought of just delaying it by an amount that would allow it to have definitely loaded (10 secs or something to be safe) but it seems like a really crappy way of doing it - there must be a slicker method.

 

Cheers though.

Hmm well if the time for the twitter feed is much more than the other stuff, maybe it's working having a little animated .gif that stays in the twitter feed until its loaded. This way your not compromising the rest of your site loading time because of the twitter feed.

 

Or,

 

When the main page has loaded, get it to fade in, and if the twitter feed hasnt loaded, get that to fade in when it eventually does.

 

Other than this I have no idea :p

  • Author

Thanks mate - worked it out (eventually). For anyone who's interested, I did this:

 

$(document).ready(function() {
   $('body').css({ opacity: 0 });
});

$(window).load(function () {
   $('body').animate({
       opacity: 1
   }, 2000, function() {
       // Animation complete.
   });
}

 

The first function kicks in immediately, while the 2nd one waits patiently for ALL elements to fully load.

 

I'm sure that's not the most elegant way of doing it, but it's worked for me.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.