這篇文章主要介紹了jquery預加載圖片的方法,涉及jQuery操作圖片的相關技巧,需要的朋友可以參考下
本文實例講述了jquery預加載圖片的方法。分享給大家供大家參考。具體如下:
這段代碼可以在頁面打開前對圖片進行預加載,這個函數非常有用,可以給用戶帶來更好的體驗。
- function preloadImages(oImageList, callback) {
- if ( typeof (oImageList) == 'object' && typeof (callback) === "function") {
- var iCallbackAfter = oImageList.length;
- var iPreloadInterval = window.setInterval(function() {
- if (iCallbackAfter === 0) {
- window.clearInterval(iPreloadInterval);
- callback();
- }
- }, 100);
- $.each(oImageList, function(iIndex, sImage) {
- oImageList[iIndex] = new Image();
- oImageList[iIndex].onload = function(oResult) {
- iCallbackAfter--;
- };
- oImageList[iIndex].onabort = function(oResult) {
- console.log(oResult);
- };
- oImageList[iIndex].onerror = function(oResult) {
- console.log(oResult);
- };
- if (!sImage.match('http://')) {
- sImage = sImage;
- }
- oImageList[iIndex].src = sImage;
- });
- }
- }
希望本文所述對大家的jQuery程序設計有所幫助。
新聞熱點
疑難解答
圖片精選