動機 原生JavaScript已經對 Image 對象提供了 onload 和 onerror 注冊事件。但在瀏覽器緩存及其他因素的影響下,用戶在使用 " /> 国产成人教育视频在线观看,99久久久无码国产精品AAA,国产人成高清在线视频99

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

Track Image Loading效果代碼分析

目的
在圖片的加載過程中,提供定義圖片加載成功或加載失敗/超時時的回調函數,并確保執行。 

動機
原生JavaScript已經對 Image 對象提供了 onload 和 onerror 注冊事件。但在瀏覽器緩存及其他因素的影響下,用戶在使用回退按鈕或者刷新頁面時 onload 事件常常未能穩定觸發。在我開發的相冊系統中,我希望圖片能根據自定義的大小顯示以免導致頁面變形,例如最寬不得超過500px,而小于500px寬度的圖片則按原大小顯示。CSS2 提供了 max-width 屬性能夠幫組我們實現這一目的。但很遺憾,挨千刀的IE6并不支持。 


IE6一個彌補的辦法就是通過注冊 img.onload 事件,待圖片加載完成后自動調整大小。以下代碼取自著名的Discuz!論壇系統4.1版本對顯示圖片的處理。 


<img src="http://img8.imagepile.NET/img8/47104p155.jpg" border="0"
onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; 
this.alt='Click here to open new windownCTRL+Mouse wheel to zoom in/out';}" 
onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new windownCTRL+Mouse wheel to zoom in/out';}" 
onclick="if(!this.resized) {return true;} else {window.open('http://img8.imagepile.NET/img8/47104p155.jpg');}" 
onmousewheel="return imgzoom(this);">

前文已述,瀏覽器并不保證事件處理函數執行。所以需要一個更穩定的方式跟蹤圖片加載過程,并執行設定的回調函數。 

實現
image.complete 屬性標示的是圖片加載狀態,其值如果為ture,則表示加載成功。圖片不存在或加載超時則值為false。利用 setInterval() 函數定時檢查該狀態則可以實現跟蹤圖片加載的狀況。代碼片斷如下: 



ImageLoader = Class.create();
ImageLoader.prototype = {
  initialize : function(options) {
    this.options = Object.extend({
      timeout: 60, //60s
      onInit: Prototype.emptyFunction,
      onLoad: Prototype.emptyFunction,
      onError: Prototype.emptyFunction
    }, options || {});
    this.images = [];
    this.pe = new PeriodicalExecuter(this._load.bind(this), 0.02);
  },
       ........
}

利用Prototype 的PeriodicalExecuter類創建一個定時器,每隔20毫秒檢查一次圖片的加載情況,并根據狀態執行 options 參數中定義的回調函數。 

使用



var loader = new ImageLoader({
  timeout: 30,
  onInit: function(img) {
    img.style.width = '100px';
  },
  onLoad: function(img) {
    img.style.width = '';
    if (img.width > 500) 
      img.style.width = '500px';
  },
  onError: function(img) {
    img.src = 'error.jpg'; //hint image
  }
});loader.loadImage(document.getElementsByTagName('img'));

上面的代碼定義圖片最初以100px顯示,加載成功后如果圖片實際寬度超過500px,則再強制定義為500px,否則顯示原大小。如果圖片不存在或加載超時(30秒為超時),則顯示錯誤圖片。 

同理,可以應用 ImageLoader 的回調函數來根據需求自定義效果,例如默認顯示loading,加載完成后再顯示原圖;圖片首先灰度顯示,加載完成后再恢復亮度等等。例如: 



//need scriptaculous effects.js
var loader = new ImageLoader({
  onInit: function(img) {
    Element.setOpacity(img, 0.5); //默認5級透明
  },
  onLoad: function(img) {
    Effect.Appear(img);  //恢復原圖顯示
  }
});


附示例中包含完整的代碼及使用pconline圖片為例的測試, 注意 范例中使用了最新的Prototype 1.5.0_rc1。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<script src="prototype1.5.0_rc1.js"></script>
<script src="validation1.5.3/effects.js"></script>
</head>

<body>
<img id="img0" src="http://img.pconline.com.cn/images/photoblog/2026024/20069/14/1158169144171.jpg" />

<img id="img1" src="http://img.pconline.com.cn/images/photoblog/2026024/20069/14/1158169158366.jpg" />

<img id="img2" src="http://img.pconline.com.cn/images/photoblog/2026024/20069/14/1158169169983_mthumb.jpg" />

<br />加載失敗測試<br />
<img id="img2" src="http://img.pconline.com.cn/images/photoblog/2026024/20069/14/000000000000.jpg" />


<script type="text/Javascript">
ImageLoader = Class.create();
ImageLoader.prototype = {

  initialize : function(options) {
    this.options = Object.extend({
      timeout: 60, //60s
      onInit: Prototype.emptyFunction,
      onLoad: Prototype.emptyFunction,
      onError: Prototype.emptyFunction
    }, options || {});
    this.images = [];
    this.pe = new PeriodicalExecuter(this._load.bind(this), 0.02);
  },

  loadImage : function() {
    var self = this;
    $A(arguments).each(function(img) {
      if (typeof(img) == 'object')
        $A(img).each(self._addImage.bind(self));
      else
        self._addImage(img);
    });
  },

  _addImage : function(img) {
    img = $(img);
    img.onerror = this._onerror.bind(this, img);
    this.options.onInit.call(this, img);
    if (this.options.timeout > 0) {
      setTimeout(this._ontimeout.bind(this, img), this.options.timeout*1000);
    }
    this.images.push(img);
    if (!this.pe.timer)
      this.pe.registerCallback();
  },

    
  _load: function() {
    this.images = this.images.select(this._onload.bind(this));
    if (this.images.length == 0) {
      this.pe.stop();
    }
  },

  _checkComplete : function(img) {
    if (img._error) {
      return true;
    } else {
      return img.complete;
    }
  },

  _onload : function(img) {
    if (this._checkComplete(img)) {
      this.options.onLoad.call(this, img);
      img.onerror = null;
      if (img._error)
        try {delete img._error}catch(e){}
      return false;
    }
    return true;
  },

  _onerror : function(img) {
    img._error = true;
    img.onerror = null;
    this.options.onError.call(this, img);
  },

  _ontimeout : function(img) {
    if (!this._checkComplete(img)) {
      this._onerror(img);
    }
  }

}

var loader = new ImageLoader({
  timeout: 30,
  onInit: function(img) {
    img.style.width = '100px';
  },
  onLoad: function(img) {
    img.style.width = '';
    if (img.width > 500) { 
      img.style.width = '500px';
    }
  },
  onError: function(img) {
    img.src = 'http://img.pconline.com.cn/nopic.gif';
  }
});

loader.loadImage(document.getElementsByTagName('img'));

/*
var loader = new ImageLoader({
  timeout: 30,
  onInit: function(img) {
    Element.setOpacity(img, 0.5);
  },
  onLoad: function(img) {
    Effect.Appear(img);
  },
  onError: function(img) {
    img.src = 'http://img.pconline.com.cn/nopic.gif';
  }
});
*/

/*
$A(document.getElementsByTagName('img')).each(
function(img) {
  img.onload = function() {
    img.style.width = '300px';
  }
}
);
*/

</script>
</body>
</html>

JavaScript技術Track Image Loading效果代碼分析,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产99在线视频 | 欧美精品一区二区三区四区 | 伊人亚洲综合网色 | 国产三级在线观看视频 | 中文字幕在线不卡精品视频99 | 我不卡影院手机在线观看 | A国产一区二区免费入口 | 色欲无码国产喷水AV精品 | 日韩a视频在线观看 | 亚洲另类国产综合在线 | 国产无遮挡无码视频在线观看不卡 | 成人手机在线 | 色偷偷男人的天堂a v | 美女扒开尿口直播 | 国偷自产视频一区二区99 | 边摸边吃奶边做激情叫床视 | 爱穿丝袜的麻麻3d漫画acg | 国产午夜精品一区二区理论影院 | 美女内射视频WWW网站午夜 | 成年妇女免费播放 | 九九热这里有精品 | 欧美国产一区二区三区激情无套 | 美国大臿蕉香蕉大视频 | 欧美黄色精品 | 欧美激情视频在线观看一区二区三区 | 脔到她哭H粗话HWWW男男动漫 | 国产精品第100页 | 国产野外无码理论片在线观看 | 九九热这里只有精品视频免费 | videossexotv极度另类 | 久久精品麻豆国产天美传媒果冻 | 西西人体大胆牲交PP6777 | 亚洲精品婷婷无码成人A片在线 | 青青精品国产自在线拍 | 天堂无码人妻精品AV一区 | 青青草原影视 | 在线成人精品国产区免费 | 一级毛片直接看 | 日韩精品 中文字幕 有码 | 国产精品亚洲精品日韩电影 | 动漫人物差差差30分钟免费看 |