Fire an event after preloading images

前端 未结 10 1851
北海茫月
北海茫月 2020-12-25 15:25

This is the code I use to preload images, I\'m not sure if it\'s the best one. My question is, how can I fire and event, for an example alert(); dialog after is has finished

相关标签:
10条回答
  • 2020-12-25 16:04

    You can use the new "$.Deferred" if you like:

    var preload = ["a.gif", "b.gif", "c.gif"];
    var promises = [];
    for (var i = 0; i < preload.length; i++) {
        (function(url, promise) {
            var img = new Image();
            img.onload = function() {
              promise.resolve();
            };
            img.src = url;
        })(preload[i], promises[i] = $.Deferred());
    }
    $.when.apply($, promises).done(function() {
      alert("All images ready sir!");
    });
    

    Might be a little risky to leave the Image objects floating around, but if so that could be fixed easily by shifting the closure. edit in fact I'll change it myself because it's bugging me :-)

    0 讨论(0)
  • 2020-12-25 16:04

    Today I needed to preload images and execute some code only after all images are loaded, but without jQuery and using Ionic2/Typescript2. Here's my solution:

    // Pure Javascript Version
    function loadImages(arrImagesSrc) {
        return new Promise(function (resolve, reject) {
            var arrImages = [];
            function _loadImage(src, arr) {
                var img = new Image();
                img.onload = function () { arr.push([src, img]); };
                img.onerror = function () { arr.push([src, null]); };
    
                img.src = src;
            }
            arrImagesSrc.forEach(function (src) {
                _loadImage(src, arrImages);
            });
            var interval_id = setInterval(function () {
                if (arrImages.length == arrImagesSrc.length) {
                    clearInterval(interval_id);
                    resolve(arrImages);
                }
            }, 100);
        });
    }
    
    // Ionic2 version
    private loadImages(arrImagesSrc: Array<string>): Promise<Array<any>> {
        return new Promise((resolve, reject) => {
          ...
          function _loadImage(src: string, arr: Array<any>) {
            ...
          }
          ...
     }
    

    You can use like this. Problematic url returns 'null'.

    loadImages(['https://cdn2.iconfinder.com/data/icons/nodejs-1/512/nodejs-512.png', 'http://foo_url'])
    .then(function(arr) {
       console.log('[???]', arr); 
    })
    
    0 讨论(0)
  • 2020-12-25 16:06

    I've seen something used to correct behavior on Masonry nice jQuery plugin (a plugin used to make nice layout composition on pages). This plugin had problems with blocks containing images and should delay his work when the images are loaded.

    First solution is to delay on the onLoad event instead of document.ready. But this event can be quite long to wait for. So they use jquery.imagesloaded.js which can detect that all images in a div are loaded; especially this very short and nice code can handle cached images which does not fire the load event sometimes.

    0 讨论(0)
  • 2020-12-25 16:12

    Check out: http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript. It uses jQuery. In the comments, your direct issue is addressed by someone and a solution was posted there. I think it'll fit your needs.

    0 讨论(0)
提交回复
热议问题