2012-11-24 41 views
0

我正在通過jquery .load動態加載一些html內容。我很難確定所有內容都已加載。文本的動態加載似乎工作正常,這是我遇到的HTML內容的加載。確保jquery .load內容已完成加載

這是頁面,如果你點擊藍色盾牌,左下角的圖片,你可以看到它不會動畫到全高度http://www.klossal.com/但如果你看這裏,你可以看到它是如何加載,它的工作原因是它沒有動態加載http://www.klossal.com/index_backup.html

這是加載HTML

$('#images').load(id +".html", function() { 

console.log('Loaded'); //Testing Purposes Only 
IMG=1; // Loaded 
animate_section(); // Attempt Animation 


}); 

腳本的一部分,這是總的腳本:

var a1, a2, a3, a4, IMG; 

$(".thumb_container_site").click(function() { 

a1=0; //Reset the Loading Variables 
a2=0; 
a3=0; 
a4=0; 
IMG=0; 

var id = $(this).attr('id'); 

$('#images').load(id +".html", function() { 

console.log('Loaded'); //Testing Purposes Only 
IMG=1; // Loaded 
animate_section(); // Attempt Animation 


}); 

$("#info_header").load(id +"_header.txt", function() { 

console.log('Loaded'); //Testing Purposes Only 
a1=1; // Loaded 
animate_section(); // Attempt Animation   

}); 

$("#content_1").load(id +"_1.txt", function() { 

console.log('Loaded'); //Testing Purposes Only 
a2=1; // Loaded 
animate_section(); // Attempt Animation   

}); 

$("#content_2").load(id +"_2.txt", function() { 

console.log('Loaded'); //Testing Purposes Only 
a3=1; // Loaded 
animate_section(); // Attempt Animation   

}); 

$("#content_3").load(id +"_3.txt", function() { 

console.log('Loaded'); //Testing Purposes Only 
a4=1; // Loaded 
animate_section(); // Attempt Animation   

}); 


}); 

任何幫助,我可以得到這將是偉大的,謝謝。

這裏是動畫功能

function animate_section() { 

if((a1===1) && (a2===1) && (a3===1) && (a4===1) && (IMG===1)){ //Animate if all thre divs are loaded 

$("#top_section").animate({ 
    height: $("#load_container").outerHeight(true) + 30 
}, 300); 
$("#grid").animate({ 
    marginTop: $("#load_container").outerHeight(true) + 300, 
    paddingBottom: 300 
}, 300); 
}     
} 

也有一個稍微不同的負載結構的第二功能,但我不認爲這應該打破任何東西。這個函數加載左上角縮略圖內容,並加載完全正確的,

$(".thumb_container_img").click(function() { 

a1=0; //Reset the Loading Variables 
a2=0; 
a3=0; 
a4=0; 
IMG=0; 

var id = $(this).attr('id'); 


$('#images').empty(); 



$("<img>", { src: 'http://www.klossal.com/' + id + ".png" }).load(function() {  
$(this).prependTo("#images"), IMG=1 }); 

$("#info_header").load(id +"_header.txt", function() { 

console.log('Loaded'); //Testing Purposes Only 
a1=1; // Loaded 
animate_section(); // Attempt Animation   

}); 

$("#content_1").load(id +"_1.txt", function() { 

console.log('Loaded'); //Testing Purposes Only 
a2=1; // Loaded 
animate_section(); // Attempt Animation   

}); 

$("#content_2").load(id +"_2.txt", function() { 

console.log('Loaded'); //Testing Purposes Only 
a3=1; // Loaded 
animate_section(); // Attempt Animation   

}); 

$("#content_3").load(id +"_3.txt", function() { 

console.log('Loaded'); //Testing Purposes Only 
a4=1; // Loaded 
animate_section(); // Attempt Animation   

}); 



}); 
+0

我想看到你的animate_section()函數 – netpoetica

+0

我更新了帖子你 – loriensleafs

回答

0

我看來像正在發生的事情是加載的文本文件時,你的負載回調被觸發,而不是當圖像本身加載。因此,對outerHeight()的調用會得到不正確的值,因爲容器元素的高度等於當時圖像的加載部分。你應該把調查的

$('#load_container img').on('load', function(){ 
    //callback or emit an event that will listen and then respond with your callback 
}); 
+0

嗯,我不太確定,我會提出,在結構,你可以幫助我? – loriensleafs

+0

你不能依賴'加載'圖像。請參閱文檔中的免責聲明。不能用於IE瀏覽器緩存中的圖片 – charlietfl

+0

,那麼你可以向我解釋哪些代碼片段放入我的代碼中? – loriensleafs

相關問題