Javascript 图像加载故障
我正在创建一个大页面,其中包含大量包含大背景图像的块,因此我认为最好是显示预加载程序,在背景中加载图像并更新加载程序(图4/20,ecc)。我的问题是:当onload事件触发我的loader disappears但我仍然看到图像加载时,这意味着它没有完全加载。为什么会发生这种情况?有什么想法吗 页面如下: 提前谢谢Javascript 图像加载故障,javascript,onload,Javascript,Onload,我正在创建一个大页面,其中包含大量包含大背景图像的块,因此我认为最好是显示预加载程序,在背景中加载图像并更新加载程序(图4/20,ecc)。我的问题是:当onload事件触发我的loader disappears但我仍然看到图像加载时,这意味着它没有完全加载。为什么会发生这种情况?有什么想法吗 页面如下: 提前谢谢 nImages = $('.slide').length; loadedImgs = 0; var bgImages = ['img/bbb.png','img/bbb2.png'
nImages = $('.slide').length;
loadedImgs = 0;
var bgImages = ['img/bbb.png','img/bbb2.png','img/bbb3.png'];
$('.slide').each(function(i){
var curSlide = $(this);
var img = new Image();
img.src = bgImages[ i % 3 ];
img.onLoad = imageLoaded(img, curSlide);
})
function imageLoaded(img, curSlide){
curSlide.css('backgroundImage', 'url(' + img.src + ')');
loadedImgs++;
if(nImages == loadedImgs){
$('#container').css('visibility','visible');
$('#loader-cont').fadeOut(1000);
}
$('.loader-inner .title').text(loadedImgs / nImages);
}
您似乎在window.onload和document.ready方面遇到了一些问题:
$(document).ready(function(){
// This will be executed when the DOM is ready.
});
$(window).load(function(){
// This will be executed when the whole document and all its
// referenced items (style sheets, scripts, images, etc.) are loaded.
});
但之前有人问过这个问题,而且回答得非常好:您似乎在window.onload和document.ready方面遇到了一些问题:
$(document).ready(function(){
// This will be executed when the DOM is ready.
});
$(window).load(function(){
// This will be executed when the whole document and all its
// referenced items (style sheets, scripts, images, etc.) are loaded.
});
但是这一点以前已经被问过了,并且得到了更好的回答:而且,模式是img.onload=someFunc;然后img.src=someFile;在尝试加载文件之前,必须告诉它加载文件时要做什么。如果映像已缓存,则很容易无法触发onload事件。它可能“起作用”(大部分),但这不是正确的方法;然后img.src=someFile;在尝试加载文件之前,必须告诉它加载文件时要做什么。如果映像已缓存,则很容易无法触发onload事件。它可能“工作”(大部分),但这不是正确的方法。谢谢elcodedocle,但添加加载动画并在窗口加载时删除它不是我想要的。我想要的是一个带有百分比的加载屏幕。不管怎样,我是这样解决的:$(img).load(function(){curSlide.css('backgroundImage','url('+img.src++'));loadedImgs++;if(nImages==loadedImgs){$('#container').css('visibility','visible');$('#loader cont cont cont.fadeOut(1000);)$('.loader internal.title').text('loading…'+((loadedImgs/nImages)*100)+'%');})谢谢elcodedocle,但添加加载动画并在窗口加载时删除它不是我想要的。我想要的是一个带有百分比的加载屏幕。无论如何,我是这样解决的:$(img.load(function(){curSlide.css('backgroundImage','url('+img.src++');loadedImgs++;if(nImages==loadedImgs){$('#container').css('visibility','visibility');$('loader cont').fadeOut(1000);}$('loader internal.title')。text('loading…+((loadedImgs/nImages)*100)+'%);)