1 Star 0 Fork 0

JqueryObjects/preload_ordered

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

基于jquery插件的图片有预跟无序加载

核心代码
/**
 * 图片预加载
 */
(function ($) {
    function Preload(imgs,options) {
        this.imgs = (typeof imgs === "string")?[imgs]:imgs;
        this.opts = $.extend({},Preload.DEFAULTS,options);
        // this._unoredered();
        if(this.opts.order === 'ordered'){
            this._ordered();
        }else {
            this._unoredered();
        }
    }
    Preload.DEFAULTS = {
        /**
         * 默认情况下无序的预加载
         */
        order:"unoredered",
        /**
         * 每张图片加载完毕后执行
         */
        each:null,
        /**
         * 所有图片加载完毕后执行
         */
        all:null,
    };
    Preload.prototype._ordered = function(){
        /**
         * 有序加载
         */
        var opts = this.opts,
            imgs = this.imgs,
            len = imgs.length,
            count = 0;
        load();
        function load() {
            var imgObj = new Image();
            $(imgObj).on("load error",function () {
                opts.each && opts.each(count);
                if(count >= len){
                    /**
                     * 所有图片已经加载完毕
                     */
                    opts.all && opts.all();

                }else {
                    load();
                }
                count++;
            });
            imgObj.src = imgs[count];
        }
    };
    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);

空文件

简介

基于jquery插件的图片有预跟无序加载 展开 收起
取消

发行版

暂无发行版

贡献者 (1)

全部

近期动态

6年前创建了仓库
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/JqueryObjects/preload_ordered.git
git@gitee.com:JqueryObjects/preload_ordered.git
JqueryObjects
preload_ordered
preload_ordered
master

搜索帮助