JS判断图片加载完成

来源:互联网 发布:沙巴知味海鲜地址 编辑:程序博客网 时间:2024/05/01 00:56
$(function(){
   // html文档加载完成后触发(不包含外部文件图片等)
})

$("img").load(function(){
   // 单张图片加载完成后触发
})

下面的代码判断所有图片加载完成:
       $(function(){
            var imgdefereds=[];  
            $('img').each(function(){  
                var dfd=$.Deferred();  
                $(this).bind('load',function(){  
                    dfd.resolve();  
                }).bind('error',function(){  
                //图片加载错误,加入错误处理  
                //  dfd.resolve();  
                })  
                if(this.complete) setTimeout(function(){  
                    dfd.resolve();  
                },1000);  
                imgdefereds.push(dfd);  
            })  
            $.when.apply(null,imgdefereds).done(function(){  
                callback(); // 全部图片加载完成触发的回调函数
            });

        })





原创粉丝点击