2011-10-28 105 views
7

我想在背景圖像完全加載後觸發事件。 該圖像由動態PHP腳本構成。當CSS背景圖像完全加載時JQuery綁定事件

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

或許我可以嘗試得到一個無形<img>當圖像加載圖像(.load()) 與無形<img>在src設置背景圖片?不知道...

謝謝你的幫助。

回答

10

你可以使用我的jQuery插件稱爲waitForImages這將幫助這...

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

否則,做手工......

var image = 'image.php', 

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

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

img.attr('src', image); 

$('#box').css('background-image', 'url(' + image + ')'); 
+0

你知道爲什麼我得到這個錯誤:錯誤:$ .isPlainObject不是一個函數嗎? (注意image.php在我們每次調用它時會生成一個新圖像) – pelelive

+0

@pelelive您是否在使用 alex

+0

謝謝亞歷克斯現在的工作。乾杯! – pelelive

-1
$("#box img").load(function() { 
    //the image elements in #box are fully loaded. 
}); 
+4

這是一個'背景 - 圖像「,而不是」img「元素。 – alex