jQuery實現的圖片預載入程式碼例項
下面是一段圖片預載入程式碼例項,現在很多圖片站都有大量的應用,當然它的好處是很明顯的,下面是一段來自於網路上的程式碼例項和大家分享一下,希望能夠給需要的朋友帶來一定的幫助。
程式碼如下:
[JavaScript] 純文字檢視 複製程式碼/* * Image preload and auto zoom * scaling 是否等比例自動縮放 * width 圖片最大高 * height 圖片最大寬 * loadpic 載入中的圖片路徑 * example $("*").LoadImage(true,w,h); */ jQuery.fn.LoadImage=function(scaling,width,height,loadpic){ if(loadpic==null)loadpic="../images/loading.gif"; return this.each(function(){ var t=$(this); var src=$(this).attr("src") var img=new Image(); //alert("Loading...") img.src=src; //自動縮放圖片 var autoScaling=function(){ if(scaling){ if(img.width>0 && img.height>0){ if(img.width/img.height>=width/height){ if(img.width>width){ t.width(width); t.height((img.height*width)/img.width); t.css("margin-top", (height-t.height())/2); } else{ t.width(img.width); t.height(img.height); t.css("margin-top", (height-t.height())/2); } } else{ if(img.height>height){ t.height(height); t.width((img.width*height)/img.height); t.css("margin-top", (height-t.height())/2); } else{ t.width(img.width); t.height(img.height); t.css("margin-top", (height-t.height())/2); } } } } } //處理ff下會自動讀取快取圖片 if(img.complete){ autoScaling(); return; } $(this).attr("src",""); var loading=$("<img alt=\"載入中...\" title=\"圖片載入中...\" src=\""+loadpic+"\" />"); t.hide(); t.after(loading); $(img).load(function(){ autoScaling(); loading.remove(); t.attr("src",this.src); t.show(); }); }); }
相關文章
- jQuery實現圖片預載入程式碼例項jQuery
- jquery圖片預載入簡單程式碼例項jQuery
- javascript圖片預載入程式碼例項JavaScript
- jQuery圖片預載入程式碼jQuery
- jquery判斷<img>圖片是否載入完成程式碼例項jQuery
- jQuery實現的圖片尺寸自適應程式碼例項jQuery
- jQuery實現的滑鼠滑過切換圖片程式碼例項jQuery
- jquery實現的圖片預載入簡單介紹jQuery
- js圖片緩衝載入程式碼例項JS
- jQuery實現的動態載入指令碼檔案程式碼例項jQuery指令碼
- jquery獲取圖片的實際尺寸大小程式碼例項jQuery
- canvas實現的圖片縮放程式碼例項Canvas
- css實現圖片灰度效果程式碼例項CSS
- jQuery實現的動態載入css和js檔案程式碼例項 [jQueryCSSJS
- jQuery選項卡切換圖片效果程式碼例項jQuery
- jQuery載入進度條例項程式碼jQuery
- 上傳圖片本地預覽例項程式碼
- 圖片上傳預覽效果程式碼例項
- js實現jquery的extend()程式碼例項JSjQuery
- 小說APP原始碼的圖片載入方式,懶載入和預載入的實現APP原始碼
- 圖片不存在使用預設圖片替代程式碼例項
- jQuery實現的tab選項卡程式碼例項jQuery
- jquery實現的選項卡效果例項程式碼jQuery
- jquery實現的人物關係圖效果程式碼例項jQuery
- javascript實現的圖片簡單切換程式碼例項JavaScript
- jQuery實現的滑鼠滑過圖片上縮彈出文字說明程式碼例項jQuery
- 在微信小程式裡實現圖片預載入元件微信小程式元件
- js圖片預載入程式碼片段分享JS
- jquery實現的分頁效果例項程式碼jQuery
- jQuery實現的隔行變色程式碼例項jQuery
- jquery實現的滑動門程式碼例項jQuery
- jQuery限制上傳圖片格式和大小程式碼例項jQuery
- jQuery實現滑動門程式碼例項jQuery
- 實現類似Pinterest 的圖片預載入功能REST
- jQuery實現的選項卡的巢狀程式碼例項jQuery巢狀
- 30行Javascript程式碼實現圖片懶載入JavaScript
- css實現圖片上下左右居中效果程式碼例項CSS
- 如何實現圖片預載入和載入進度條