基于jquery的防止大圖片撐破頁面的實現(xiàn)代碼(立即縮放)
更新時間:2011年10月24日 01:24:51 作者:
這篇文章將根據(jù)此寫一個應(yīng)用:讓圖片未加載完畢就實現(xiàn)按比例自適應(yīng),防止圖片撐破布局(尤其是外鏈圖片)
為了防止圖片撐破布局,最常見的仍然是通過onload后獲取圖片尺寸再進(jìn)行調(diào)整,所以加載過程中仍然會撐破。而Qzone日志的圖片在此進(jìn)行了改進(jìn),onload完畢后才顯示原圖。我以前用onload寫過一個小例子:http://www.planeart.cn/?p=1022
通過imgReady可以跨瀏覽器在dom ready就可以實現(xiàn)圖片自適應(yīng),無需等待img加載,代碼如下:
(3-17修復(fù)網(wǎng)友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');
// 獲取圖片頭尺寸數(shù)據(jù)后立即調(diào)整圖片
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);
/**
* 圖片頭數(shù)據(jù)加載就緒事件
* @see http://www.planeart.cn/?p=1121
* @param {String} 圖片路徑
* @param {Function} 尺寸就緒 (參數(shù)1接收width; 參數(shù)2接收height)
* @param {Function} 加載完畢 (可選. 參數(shù)1接收width; 參數(shù)2接收height)
* @param {Function} 加載錯誤 (可選)
*/
var imgReady = (function () {
var list = [], intervalId = null,
// 用來執(zhí)行隊列
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;
// 如果圖片被緩存,則直接返回緩存數(shù)據(jù)
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 ||
// 如果圖片已經(jīng)在其他地方加載可使用面積檢測
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動畫會循環(huán)執(zhí)行onload,置空onload即可
img = img.onload = img.onerror = null;
};
// 加入隊列中定期執(zhí)行
if (!check.end) {
list.push(check);
// 無論何時只允許出現(xiàn)一個定時器,減少瀏覽器性能損耗
if (intervalId === null) intervalId = setInterval(tick, 40);
};
};
})();
})(jQuery);
autoIMG壓縮后:1.74kb,兼容:Chrome | Firefox | Sifari | Opera | IE6 | IE7 | IE8 | …
調(diào)用演示:$(‘#demo p').autoIMG()
同樣,令人愉悅的DEMO地址在這里:http://demo.jb51.net/js/2011/autoimg/
后記:雖然有了上一篇文章imgReady技術(shù)的鋪墊,我以為會很簡單的實現(xiàn)這個圖片自適應(yīng)插件,可是過程中卻在webkit內(nèi)核的瀏覽器碰了一鼻子灰,后來才得知webkit有BUG未修復(fù),webkit無法無法中斷img下載。我折騰許久后更新了imgReady函數(shù)與這個例子。
打包下載地址
通過imgReady可以跨瀏覽器在dom ready就可以實現(xiàn)圖片自適應(yīng),無需等待img加載,代碼如下:
(3-17修復(fù)網(wǎng)友crossyou 指出的一處錯誤,并且新版本去掉了替換圖片)
復(fù)制代碼 代碼如下:
// 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');
// 獲取圖片頭尺寸數(shù)據(jù)后立即調(diào)整圖片
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);
/**
* 圖片頭數(shù)據(jù)加載就緒事件
* @see http://www.planeart.cn/?p=1121
* @param {String} 圖片路徑
* @param {Function} 尺寸就緒 (參數(shù)1接收width; 參數(shù)2接收height)
* @param {Function} 加載完畢 (可選. 參數(shù)1接收width; 參數(shù)2接收height)
* @param {Function} 加載錯誤 (可選)
*/
var imgReady = (function () {
var list = [], intervalId = null,
// 用來執(zhí)行隊列
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;
// 如果圖片被緩存,則直接返回緩存數(shù)據(jù)
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 ||
// 如果圖片已經(jīng)在其他地方加載可使用面積檢測
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動畫會循環(huán)執(zhí)行onload,置空onload即可
img = img.onload = img.onerror = null;
};
// 加入隊列中定期執(zhí)行
if (!check.end) {
list.push(check);
// 無論何時只允許出現(xiàn)一個定時器,減少瀏覽器性能損耗
if (intervalId === null) intervalId = setInterval(tick, 40);
};
};
})();
})(jQuery);
autoIMG壓縮后:1.74kb,兼容:Chrome | Firefox | Sifari | Opera | IE6 | IE7 | IE8 | …
調(diào)用演示:$(‘#demo p').autoIMG()
同樣,令人愉悅的DEMO地址在這里:http://demo.jb51.net/js/2011/autoimg/
后記:雖然有了上一篇文章imgReady技術(shù)的鋪墊,我以為會很簡單的實現(xiàn)這個圖片自適應(yīng)插件,可是過程中卻在webkit內(nèi)核的瀏覽器碰了一鼻子灰,后來才得知webkit有BUG未修復(fù),webkit無法無法中斷img下載。我折騰許久后更新了imgReady函數(shù)與這個例子。
打包下載地址
您可能感興趣的文章:
- jQuery實現(xiàn)等比例縮放大圖片讓大圖片自適應(yīng)頁面布局
- jQuery+css實現(xiàn)的點擊圖片放大縮小預(yù)覽功能示例【圖片預(yù)覽 查看大圖】
- jQuery實現(xiàn)鼠標(biāo)滑過商品小圖片上顯示對應(yīng)大圖片功能【測試可用】
- jQuery實現(xiàn)鼠標(biāo)滑過預(yù)覽圖片大圖效果的方法
- jQuery實現(xiàn)大圖輪播
- jQuery實現(xiàn)的小圖列表,大圖展示效果幻燈片示例
- jquery插件jquery.LightBox.js實現(xiàn)點擊放大圖片并左右點擊切換效果(附demo源碼下載)
- jQuery實現(xiàn)點擊小圖片淡入淡出顯示大圖片特效
- jQuery實現(xiàn)點擊查看大圖并以彈框的形式居中
- 基于jQuery插件實現(xiàn)點擊小圖顯示大圖效果
- jquery實現(xiàn)移動端點擊圖片查看大圖特效
- jQuery實現(xiàn)點擊小圖顯示大圖代碼分享
- jquery 圖片點擊放大預(yù)覽功能詳解
相關(guān)文章
推薦40個非常優(yōu)秀的jQuery插件和教程【系列三】
jQuery 在如今的 Web 開發(fā)項目中扮演著重要角色,jQuery 以其插件眾多、獨特、輕量以及支持大規(guī)模的網(wǎng)站開發(fā)聞名。本文大家分享40個實用的 jQuery 插件,可以根據(jù)您的項目需要來選擇使用2011-11-11JQuery+drag.js上傳圖片并且實現(xiàn)圖片拖曳
這篇文章主要介紹了JQuery+drag.js上傳圖片并且實現(xiàn)圖片拖曳,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11基于JQuery的模擬蘋果桌面Dock效果(穩(wěn)定版)
之所以將它命名為穩(wěn)定版,是因為之前已經(jīng)分享了一個初級版本的,之前的初級版中存在很多bug?,F(xiàn)在經(jīng)過反復(fù)琢磨、實驗,修復(fù)了之前版本存在的很多不足之處,就算鼠標(biāo)快速的滑動依然表現(xiàn)的很穩(wěn)定2012-10-10JQuery一種取同級值的方式(比如你在GridView中)
JQuery一種取同級值的方式 比如你在GridView中,實現(xiàn)代碼如下,需要的朋友可以參考下2012-03-03解決Jquery鼠標(biāo)經(jīng)過不?;瑒拥膯栴}
在鼠標(biāo)經(jīng)過的時候不停的顯示隱藏html元素,正確的寫法應(yīng)該是下面這樣的,需要的朋友可以參考下2014-03-03