简体   繁体   English

jQuery插件预加载图像

[英]jquery plugin preloading images

I made a jquery plugin for displaying images on: http://jsfiddle.net/wfARj/ 我制作了一个用于在以下位置显示图像的jquery插件: http : //jsfiddle.net/wfARj/

One of his feature is preloading images: 他的功能之一是预加载图像:

function preload(){
        for (var i = firstIndex; i <= lastIndex; i++) {
            images[i] = new Image();
            images[i].onload = function(response){
                console.log(this.src + ' is successfully loaded!');
            }
            images[i].src = $(selector).eq(i).attr('href');
        }

} 

Problem is following, when some photo is large(>5MB), site loading i too slow. 接下来的问题是,当某些照片很大(> 5MB)时,网站加载速度太慢。

I try with: 我尝试:

function preload(){
    setTimeout(function(){
            for(var i = firstIndex;i <= lastIndex;i++)
            {
                images[i] = new Image();
                images[i].onload = function(response){
                    console.log(this.src + ' is successfully loaded!');
                }
                if(elementType=='A') images[i].src = $(selector).eq(i).attr('href');
                else if(elementType=='IMG') images[i].src =            $(selector).eq(i).attr('src');
                else images[i].src = '';
            }
        }, 300);
}

But problem is still there... I want preload images in background, after page load. 但是问题仍然存在。我想在页面加载后在后台预加载图像。 Detect full page load with window.bind load is not secure option. 使用window.bind加载不是安全选项来检测整页加载。 How i can make fix that? 我该如何解决?

thanks 谢谢

but the settimeout inside the for loop closure so each load call gets its own timeout. 但是在for循环关闭中设置了settimeout,因此每个加载调用都会获得自己的超时。

function preload(){
            for(var i = firstIndex;i <= lastIndex;i++)
            {
                setTimeout(function(){
                    images[i] = new Image();
                    images[i].onload = function(response){
                         console.log(this.src + ' is successfully loaded!');
                    }
                    if(elementType=='A') images[i].src = $(selector).eq(i).attr('href');
                    else if(elementType=='IMG') images[i].src =   $(selector).eq(i).attr('src');
                    else images[i].src = '';
                }, 300);
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM