简介 目录 评价 推荐
  • _OuYanG 2020-02-05

    有序预加载插件部分

    截图
    0赞 · 0采集
  • _OuYanG 2020-02-05

    预加载插件

    截图
    0赞 · 0采集
  • aSuncat 2018-04-21

    (function($) {

      // 面向对象

      function PreLoad(imgs, options) {

        this.imgs = (typeof imgs === 'string') ? [imgs] : imgs;

        this.opts = $.extend({}, PreLoad.DEFAULTS, options); // options和PreLoad.DEFAULTS这2个对象融合,生成新的对象,将新对象返回给opts保存下来。

        if (this.opts.order === 'ordered') {

          this._ordered();

        } else {

          this._unordered(); // _表示只在函数内部使用,而不在外部调用

        }

      }

      PreLoad.DEFAULTS = {

        order: 'unordered', // 无序预加载

        each: null, // 每一张图片加载完毕后执行

        all: null  // 所有图片加载完毕后执行

      };

      PreLoad.prototype._ordered = function() { //方法写在原型链上

        var opts = this.opts, // 保存为局部变量

            imgs = this.imgs,

            len = imgs.length,

            count = 0;

        var imgObj = new Image();

        load();

        function load() {

          $(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._unordered = 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); // 如果opts.each存在,执行opts.each方法

            if (count >= len - 1) {

              opts.all && opts.all();

            }

            count ++;

          });

          imgObj.src = src;

        });

      };

      // 插件:①附在$.fn上(要选择一个元素);②直接跟在jquery对象,是一个工具函数。(工具方法)

      // $.fn.extend -> $('#img').preload();

      $.extend({

        preload: function(imgs, opts) {

          new PreLoad(imgs, opts);

        }

      });

    })(jQuery);


    1赞 · 1采集
  • Wendy_22 2018-03-25

    有序无序预加载整合

    截图
    1赞 · 1采集
  • qq_你瘦该你拽_0 2017-11-24
    有序加载
    截图
    0赞 · 1采集
  • 小平果 2017-08-13
    哈哈,
    截图
    0赞 · 0采集
数据加载中...
开始学习 免费