Jquery预载大图像

时间:2014-04-07 02:45:43

标签: javascript jquery

如何仅在加载此类图像时加载页面?基础不起作用,键入:

$('#IDdaImagem').on('load',function(){ 
})

是一个背景图片,并且可以使用jquery插件在其他分辨率中打开,backstretch,它使用带有类backstretch的div,因为我在网站内容之前带有2mb的背景图像?

我测试这个并且工作,但不是我给出的项目,当时出现,它仅在警报中起作用。

<script>
$('.backstretch img').load(function() {
    alert('done loading image');
    $("#corpo").show();
});
</script>

2 个答案:

答案 0 :(得分:1)

你的例子让我觉得你不想&#34; preload&#34;必须使用图像,但想要在运行脚本之前等待图像加载。你可以使用像waitForImages jQuery这样的插件。

https://github.com/alexanderdickson/waitForImages

答案 1 :(得分:0)

尝试下面给出的代码:

function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
        // Alternatively you could use:
        // (new Image()).src = this;
    });
}

// Usage:

preload([
    'img/imageName.jpg',
    'img/anotherOne.jpg',
    'img/blahblahblah.jpg'
]);

使用Jquery插件:

$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}

// Usage:

$(['img1.jpg','img2.jpg','img3.jpg']).preload();