zoukankan      html  css  js  c++  java
  • 判断图片是否加载完成

    最简单:

    document.getElementById("img2").onload=function(){}
    

    性能优化:

    function loadImage(url, callback) {
     var img = new Image(); //创建一个Image对象,实现图片的预下载
     img.src = url;
     
     if(img.complete) { // 如果图片已经存在于浏览器缓存,直接调用回调函数
         callback.call(img);
         return; // 直接返回,不用再处理onload事件
        }
     img.onload = function () { //图片下载完毕时异步调用callback函数。
            callback.call(img);//将回调函数的this替换为Image对象
        };
    };
    

    对图片多个判断

    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();
    });
    

      

  • 相关阅读:
    iOS.CM5.CM4.CM2
    iOS.Library.Architecture
    iOS.Info.plist
    iOS.ARM-Assembly
    Tools.Png.Compression
    MacDev.GetArchOfLibrary
    iOS.C
    iOS.Notification.Bar.Color
    iOS.-.cxx_destruct
    iOS.UITableView.SectionIndex
  • 原文地址:https://www.cnblogs.com/xupeiyu/p/5044349.html
Copyright © 2011-2022 走看看