基於jquery的防止大圖片撐破頁面的實現代碼(立即縮放)

來源:互聯網
上載者:User

為了防止圖片撐破布局,最常見的仍然是通過onload後擷取圖片尺寸再進行調整,所以載入過程中仍然會撐破。而Qzone日誌的圖片在此進行了改進,onload完畢後才顯示原圖。我以前用onload寫過一個小例子:http://www.planeart.cn/?p=1022

通過imgReady可以跨瀏覽器在dom ready就可以實現圖片自適應,無需等待img載入,代碼如下:
(3-17修複網友crossyou 指出的一處錯誤,並且新版本去掉了替換圖片)
複製代碼 代碼如下:
// jquery.autoIMG.js - 2010-04-02 - Tang Bin - http://planeArt.cn/ - MIT Licensed
(function ($) {
// 檢測是否支援css2.1 max-width屬性
var isMaxWidth = 'maxWidth' in document.documentElement.style,
// 檢測是否IE7瀏覽器
isIE7 = !-[1,] && !('prototype' in Image) && isMaxWidth;

$.fn.autoIMG = function () {
var maxWidth = this.width();

return this.find('img').each(function (i, img) {
// 如果支援max-width屬性則使用此,否則使用下面方式
if (isMaxWidth) return img.style.maxWidth = maxWidth + 'px';
var src = img.src;

// 隱藏原圖
img.style.display = 'none';
img.removeAttribute('src');

// 擷取圖標題尺寸資料後立即調整圖片
imgReady(src, function (width, height) {
// 等比例縮小
if (width > maxWidth) {
height = maxWidth / width * height,
width = maxWidth;
img.style.width = width + 'px';
img.style.height = height + 'px';
};
// 顯示原圖
img.style.display = '';
img.setAttribute('src', src);
});

});
};

// IE7縮放圖片會失真,採用私人屬性通過三次插值解決
isIE7 && (function (c,d,s) {s=d.createElement('style');d.getElementsByTagName('head')[0].appendChild(s);s.styleSheet&&(s.styleSheet.cssText+=c)||s.appendChild(d.createTextNode(c))})('img {-ms-interpolation-mode:bicubic}',document);

/**
* 圖標題資料載入就緒事件
* @see http://www.planeart.cn/?p=1121
* @param {String} 圖片路徑
* @param {Function} 尺寸就緒 (參數1接收width; 參數2接收height)
* @param {Function} 載入完畢 (可選. 參數1接收width; 參數2接收height)
* @param {Function} 載入錯誤 (可選)
*/
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 check, width, height, newWidth, newHeight,
img = new Image();

img.src = url;

// 如果圖片被緩衝,則直接返回快取資料
if (img.complete) {
ready(img.width, img.height);
load && load(img.width, img.height);
return;
};

// 檢測圖片大小的改變
width = img.width;
height = img.height;
check = function () {
newWidth = img.width;
newHeight = img.height;
if (newWidth !== width || newHeight !== height ||
// 如果圖片已經在其他地方載入可使用面積檢測
newWidth * newHeight > 1024
) {
ready(newWidth, newHeight);
check.end = true;
};
};
check();

// 載入錯誤後的事件
img.onerror = function () {
error && error();
check.end = true;
img = img.onload = img.onerror = null;
};

// 完全載入完畢的事件
img.onload = function () {
load && load(img.width, img.height);
!check.end && check();
// IE gif動畫會迴圈執行onload,置空onload即可
img = img.onload = img.onerror = null;
};

// 排入佇列中定期執行
if (!check.end) {
list.push(check);
// 無論何時只允許出現一個定時器,減少瀏覽器效能損耗
if (intervalId === null) intervalId = setInterval(tick, 40);
};
};
})();

})(jQuery);

autoIMG壓縮後:1.74kb,相容:Chrome | Firefox | Sifari | Opera | IE6 | IE7 | IE8 | …

調用示範:$(‘#demo p').autoIMG()

同樣,令人愉悅的DEMO地址在這裡:http://demo.jb51.net/js/2011/autoimg/

後記:雖然有了上一篇文章imgReady技術的鋪墊,我以為會很簡單的實現這個圖片自適應外掛程式,可是過程中卻在webkit核心的瀏覽器碰了一鼻子灰,後來才得知webkit有BUG未修複,webkit無法無法中斷img下載。我折騰許久後更新了imgReady函數與這個例子。

打包

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.