# preload **Repository Path**: JqueryObjects/preload ## Basic Information - **Project Name**: preload - **Description**: 图片预加载之无序加载 - **Primary Language**: JavaScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-08-05 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 图片预加载之无序加载 效果如下: ![](images/GIF.gif) 基于jquery写的预加载功能插件 ```angularjs /** * 图片预加载 */ (function ($) { function Preload(imgs,options) { this.imgs = (typeof imgs === "string")?[imgs]:imgs; this.opts = $.extend({},Preload.DEFAULTS,options); this._unoredered(); } Preload.DEFAULTS = { /** * 每张图片加载完毕后执行 */ each:null, /** * 所有图片加载完毕后执行 */ all:null, }; Preload.prototype._unoredered = function () { /** * 无序加载 */ var imgs = this.imgs, opts = this.opts, count = 0, len = imgs.length; $.each(imgs,function (i,src) { if(typeof src != "string") return; var imgObj = new Image(); $(imgObj).on("load error",function () { opts.each && opts.each(count); if(count >= len -1){ opts.all && opts.all(); } count ++; }); imgObj.src = src; }); }; $.extend({ preload:function (imgs,opts) { new Preload(imgs,opts) } }) })(jQuery); ``` 页面调用 ```angularjs var imgs = [ "images/1.jpg", "images/2.jpg", "images/3.jpg", "images/4.jpg", "images/5.jpg", "images/6.jpg", "images/7.jpg" ], index = 0, len = imgs.length, $progress = $(".progress"); $.preload(imgs,{ each:function (count) { $progress.html(Math.round((count + 1)/len * 100) + "%"); }, all:function () { $(".loading").hide(); document.title = "1/"+len; } }); $(".btn").click(function () { if($(this).data("control") === "prev"){ //上一张 index = Math.max(0,--index); }else { index = Math.min(len - 1,++index) } document.title = (index + 1) + "/" + len; $("#img").attr("src",imgs[index]); }) ``` html+css ````angularjs 图片预加载之无序加载

上一页 下一页

0%

````