您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

jquery预加载图片的方法

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   本文实例讲述了jquery预加载图片的方法。分享给大家供大家参考。具体如下:

  这段代码可以在页面打开前对图片进行预加载,这个函数非常有用,可以给用户带来更好的体验。

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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程序设计有所帮助。