问答详情
源自:-

预加载完成后程序继续执行,没有停止请问是什么原因?

http://img.mukewang.com/5a7bf011000191d206460396.jpg

http://img2.mukewang.com/5a7bf01200016c0c07550512.jpg

代码按老师说的敲,求大神指点。。。。


提问者:aR_Easun 2018-02-08 14:38

个回答

  • aR_Easun
    2018-02-08 14:39:50


    //图片预加载_无序加载

    (function($) {

    function PreLoad(imgs, options) {

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

    this.opts = $.extend({}, PreLoad.DEFAULTS, options);

    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.lenght,

    count = 0;

    load();

    //有序预加载

    function load() {

    var imgObj = new Image();

    $(imgObj).on('load error', function() {

    opts.each && opts.each(cuont); 

    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);

    if(count >= len - 1) {

    opts.all && opts.all();

    }

    count++;

    })

    imgObj.src = src;

    });

    };

    $.extend({

    preload: function(imgs, opts) {

    new PreLoad(imgs, opts);

    }

    })

    })(jQuery);