【问题标题】:making sure jquery .load content is finished loading确保 jquery .load 内容已完成加载
【发布时间】:2012-11-24 02:41:29
【问题描述】:

我正在通过 jquery .load 动态加载一些 html 内容。我很难确保所有内容都已加载。文本的动态加载似乎工作正常,这是我遇到问题的 html 内容的加载。

这是页面,如果您单击左下角的蓝色盾牌,您会看到它没有动画到全高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         

});



});

【问题讨论】:

  • 我想看看你的 animate_section() 函数

标签: jquery load


【解决方案1】:

在我看来,发生的事情是加载文本文件时触发了加载回调,而不是加载图像本身时。因此,您对 outerHeight() 的调用得到了不正确的值,因为容器元素的高度等于当时图像的加载部分。您应该调查放置一个

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

【讨论】:

  • 嗯,我不确定我应该把它放在结构中的什么位置,你能帮我吗?
  • 您不能依赖load 获取图像。请参阅文档中的免责声明。缓存中的图像时不适用于 IE
  • 那么你能解释一下在哪里把代码 sn-p 放到我的代码中吗?
  • 那么有没有其他方法可以加载它?
  • 嘿-不想在这里刻薄-我确实尝试过查看您的代码,看看是否可以找到适合该架构的位置。您的代码非常混乱,并且没有遵循任何最佳实践。诸如将语义标记与后面的动态代码分开之类的事情还没有到位。处理您的代码意味着从元素 ID 到代码片段来回切换。我建议研究语义标记、事件委托和冒泡,以及 JSON 而不是 .txt 文件 - 将其降低可能有助于您找到更顺畅的解决当前问题的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 2023-01-02
  • 1970-01-01
相关资源
最近更新 更多