views:

432

answers:

5

I have the following CSS class

.bg {
   background-image: url('bg.jpg');
   display: none;
}

that I'm applying on a TD tag.

My question is how can I tell with JavaScript/jQuery that the background image finished loading?

Thank you.

UPDATE: added display property. Since my main objective it toggle it into view.

+2  A: 

Give the class to a div with visibility:hidden at the initial page load. That way, it'll already be in the browser cache when you assign the class to your table cell.

Gausie
+1  A: 

The only way I know of to do this is to load the image using Javascript, and then set that image as the backgroud.

For example:

var bgImg = new Image();
bgImg.onload = function(){
   myDiv.style.backgroundImage = 'url(' + bgImg.src + ')';
};
bgImg.src = imageLocation;
Jamie Dixon
For some weird reason the onload only fires when the imageLocation actually takes some time to load, like from the web. It doesn't work in local testing with 0 loading time.
thedp
you could set a timeout for the `bgImg.src = imageLocation;`, but I'd assume most people won't be on your computer, :P
Dan Beam
+2  A: 

This article may help you

Basically select the background-image and do the check to see it's loaded.

AJM
I don't think this can be done when the background's initial state is display:none;
thedp
+2  A: 

@Jamie Dixon - he didn't say he wanted to do anything with the background image, just know when it's loaded...

$(function( )
{
    var a = new Image;
    a.onload = function( ){ /* do whatever */ };
    a.src = $( 'body' ).css( 'background-image' );
});
Dan Beam
Will it work if the element on which I applied background has the property - display:none; ?
thedp
yes, as far as I know this doesn't matter, it's just grabbing the style rules and forcing the image to preload (essentially), when it is done preloading it fires an onLoad event.however you don't want to do this if you want to lazy load a large image (as it may slow things down)
Dan Beam
I'm having trouble to make it work.Here is my style: background-image: url('../Images/bg/xxx.jpg');The a.src gives me the image location with the url('');It seems that the onLoad is never fired. (I also tried onload)
thedp
hey thedp, I just edited my code. it wasn't working for me, though I swear I was when I posted it. try the new version.
Dan Beam
A: 

ok now if i want to resize the image and then set it as my background....i.e., after loading it will resize the image as per bowser size and then set it as bachground....

<SCRIPT language=JavaScript>

function stretchBG() {

//get background image
var bgImg = new Image();
//change line below to be relative path to your background image
bgImg.src = "/Bliss.bmp";

//set image to have same dimensions as current window
var imgX = self.outerwidth;
var imgY = self.outerheight;
bgImg.width = imgX;

bgImg.height = imgY;

//assign image to background of page

document.body.style.backgroundImage = 'url(' + bgImg.src + ')';
document.body.style.backgroundRepeat = "no-repeat";


}
Ayan