在动画制作之前等待预先添加图像

时间:2012-11-23 18:41:44

标签: jquery prepend

我有一个图像,我正在前面的div,然后div被动画到一个新的高度,问题是,或者至少我认为是,页面不等待图像是在抓住高度和动画之前加载,我不知道如何做到这一点。

我现在已经设置好了,所以页面等到文本加载完毕但我不知道如何也包含图像部分。这是具体的     $( '#图像')空();

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

这就是整个事情:

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

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

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


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



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


$("#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);   
}                
}

1 个答案:

答案 0 :(得分:2)

您需要使用jQuery中的load()函数来检查图像是否已加载。然后你可以在前面添加你想要的所有内容

$('img').attr('src', 'image.jpg').load(function() { $(this).prependTo(...) })