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(); }); }); }
相關文章
- 小說APP原始碼的圖片載入方式,懶載入和預載入的實現APP原始碼
- Python實現簡單網頁圖片抓取完整程式碼例項Python網頁
- 30行Javascript程式碼實現圖片懶載入JavaScript
- 如何實現圖片預載入和載入進度條
- canvas載入效果程式碼例項Canvas
- 預載入圖片
- 圖片懶載入實現
- 100多行程式碼實現js或者jquery版的類似juejin的預覽圖片功能行程JSjQuery
- jQuery.map()方法程式碼例項jQuery
- jQuery tab選項卡效果程式碼例項jQuery
- 圖片預載入和懶載入
- 圖片懶載入js實現JS
- 微信小程式--實現圖片懶載入(lazyload)微信小程式
- jQuery利用name匹配元素程式碼例項jQuery
- 滾動載入圖片(懶載入)實現原理
- jquery.idTabs.min.js選項卡程式碼例項jQueryJS
- [譯] 原生實現圖片懶載入
- React如何實現圖片懶載入React
- 實現圖片懶載入(throttle, debounce)
- 圖片預載入,圖片懶載入,和jsonp中的一個疑問JSON
- jQuery點選滑出層效果程式碼例項jQuery
- jQuery table表格隔行換色程式碼例項jQuery
- 實現圖片懶載入的三種方式
- jQuery實現的表格展開伸縮效果例項jQuery
- 如何實現一個圖片載入框架框架
- html實現簡單ListViews效果的例項程式碼HTMLView
- jQuery遍歷array陣列元素程式碼例項jQuery陣列
- Android 載入網路圖片 以及實現圓角圖片效果Android
- 頁面圖片預載入與懶載入策略
- jQuery 例項jQuery
- JS 預編譯程式碼例項分析JS編譯
- vuejs實現新增tag標籤程式碼例項VueJS
- 例項程式碼分享Python實現Linux監控PythonLinux
- node實現登入圖片驗證碼
- 5種PHP生成圖片驗證碼例項PHP
- 【例項】使用GD庫生成圖片驗證碼
- Django登入(含隨機生成圖片驗證碼)註冊例項Django隨機
- ECMAScript擴充套件 -12 【圖片的預載入與懶載入】套件
- jQuery點選文字框清除內容程式碼例項jQuery