7

I would like to trigger an event after a background-image is FULLY loaded. The image is made from a dynamic PHP script.

$("#box").css("background-image","url(image.php)");

maybe I can try to get the image in a invisible <img> and when the image load (.load()) set the background-image with the src of that invisible <img> ? not sure...

thank you for your help.

mu is too short
  • 396,305
  • 64
  • 779
  • 743
pelelive
  • 567
  • 1
  • 5
  • 14

2 Answers2

10

You could use my jQuery plugin called waitForImages that would help with this...

$("#box").css("background-image", "url(image.php)").waitForImages({
   waitForAll: true,
   finished: function() {
       // Background image has loaded.
   }
});

Otherwise, to do it manually...

var image = 'image.php',

    img = $('<img />');

img.bind('load', function() {
     // Background image has loaded.
});

img.attr('src', image);

$('#box').css('background-image', 'url(' + image + ')');
alex
  • 438,662
  • 188
  • 837
  • 957
-1
$("#box img").load(function() {  
    //the image elements in #box are fully loaded.
});
Johan
  • 31,613
  • 48
  • 166
  • 272