javascript圖片預先載入執行個體分析,javascript執行個體分析

來源:互聯網
上載者:User

javascript圖片預先載入執行個體分析,javascript執行個體分析

本文執行個體講述了javascript圖片預先載入的方法。分享給大家供大家參考。具體如下:

lightbox類效果為了讓圖片置中顯示而使用預先載入,需要等待完全載入完畢才能顯示,體驗不佳(如filick相簿的全屏效果)。javascript無法擷取img檔案頭資料,真的是這樣嗎?本文通過一個巧妙的方法讓javascript擷取它。

這是大部分人使用預先載入擷取圖片大小的例子:

var imgLoad = function (url, callback) {  var img = new Image();  img.src = url;  if (img.complete) {    callback(img.width, img.height);  } else {    img.onload = function () {      callback(img.width, img.height);      img.onload = null;    };  };};

JavaScript代碼:

// 更新:// 05.27: 1、保證回調執行順序:error > ready > load;2、回呼函數this指向img本身// 04-02: 1、增加圖片完全載入後的回調 2、提高效能/** * 圖標題資料載入就緒事件 - 更快擷取圖片尺寸 * @version 2011.05.27 * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a> TangBin * <a href="http://my.oschina.net/see" class="referer" target="_blank">@see</a>   http://www.planeart.cn/?p=1121 * @param  {String}  圖片路徑 * @param  {Function} 尺寸就緒 * @param  {Function} 載入完畢 (可選) * @param  {Function} 載入錯誤 (可選) * @example imgReady('http://www.google.com.hk/intl/zh-CN/images/logo_cn.png', function () {    alert('size ready: width=' + this.width + '; height=' + this.height);  }); */var imgReady = (function () {  var list = [], intervalId = null,  // 用來執行隊列  tick = function () {    var i = 0;    for (; i < list.length; i++) {      list[i].end ? list.splice(i--, 1) : list[i]();    };    !list.length && stop();  },  // 停止所有定時器隊列  stop = function () {    clearInterval(intervalId);    intervalId = null;  };  return function (url, ready, load, error) {    var onready, width, height, newWidth, newHeight,      img = new Image();    img.src = url;    // 如果圖片被緩衝,則直接返回快取資料    if (img.complete) {      ready.call(img);      load && load.call(img);      return;    };    width = img.width;    height = img.height;    // 載入錯誤後的事件    img.onerror = function () {      error && error.call(img);      onready.end = true;      img = img.onload = img.onerror = null;    };    // 圖片尺寸就緒    onready = function () {      newWidth = img.width;      newHeight = img.height;      if (newWidth !== width || newHeight !== height ||        // 如果圖片已經在其他地方載入可使用面積檢測        newWidth * newHeight > 1024      ) {        ready.call(img);        onready.end = true;      };    };    onready();    // 完全載入完畢的事件    img.onload = function () {      // onload在定時器時間差範圍內可能比onready快      // 這裡進行檢查並保證onready優先執行      !onready.end && onready();      load && load.call(img);      // IE gif動畫會迴圈執行onload,置空onload即可      img = img.onload = img.onerror = null;    };    // 排入佇列中定期執行    if (!onready.end) {      list.push(onready);      // 無論何時只允許出現一個定時器,減少瀏覽器效能損耗      if (intervalId === null) intervalId = setInterval(tick, 40);    };  };})();

調用例子:

imgReady('http://www.google.com.hk/intl/zh-CN/images/logo_cn.png', function () {  alert('size ready: width=' + this.width + '; height=' + this.height);});

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.