0

I am trying to find out whether it is possible to check if a background image has been loaded before fading in the content? The background image is set in the css so if the user doesnt have js turned on they are still able to view the site.

I have tried $(".quick-load").load(function () { $("#content").fadeIn(); });

I have also used .ready() but none have had any effect.

The exact problem i am having is that the image will flicker on some occasions.

Any ideas would be helpful, thanks.

2
  • one question... you are loading the bg through css so that it loads even when js is disabled but then what about the content? you don't care about that? :) Commented Apr 22, 2011 at 11:02
  • i have a special no js file with html5. so if no js is detected i have display: block on the content Commented Apr 22, 2011 at 11:08

2 Answers 2

2

I think you should try a slightly different approach, which is to load the image first in a hidden image tag, then on the load event do your cool fading

<img src="background.png" alt="" id="background" 
style="visibility:hidden;position:absolute" />

Then do:

$('#background').load(function() {
   $("#content").fadeIn(); }
});

This will work because the browser caches the image on load and it will be available to use in the background. Just make sure that the source for your invisible image and your background image are identical.

Sign up to request clarification or add additional context in comments.

5 Comments

ahh so you fake the browser to load that image but by the time that invisible image is loaded my background image would have loaded and it wont do any flicker?
yeah, that is exactly it. If you search for image preloading too, that is another strategy you could use.
ah wicked. thanks for that and thanks for the other idea. im going to give your tip a go first:)
What about if you need to strictly use a background (because of its positioning properties; with the image I'd have to code it myself with jquery)? There should be a way to check if a source finished the download.
That looks tempting to use but unfortunately this function is inconsistent across browsers when used on images: api.jquery.com/load-event
0

You can "preload" images by using a hidden <img>.

$('<img/>').attr('src', 'http://picture.com/pic.png').load(function() {
    $("#content").css({"background-image":"http://picture.com/pic.png"
                 .fadeIn();
});

How can I check if a background image is loaded?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.