jQuery isotope on first load doesn't work, How do I wait for all resources/images to be loaded?

后端 未结 7 1902
一个人的身影
一个人的身影 2021-01-17 16:57

I\'ve got something I\'ve put together using jQuery isotope here.. http://jsbin.com/eziqeq/6/edit

It seems to work in general but on first load, of a new tab, the Is

7条回答
  •  鱼传尺愫
    2021-01-17 17:30

    Quick pure JavaScript example to check if all imagery has loaded before initialising the isotope library.

    jQuery(document).ready(function($){
        var imgs = document.images,
            len = imgs.length,
            counter = 0;
    
        [].forEach.call( imgs, function( img ) {
            if(img.complete)
              incrementCounter();
            else
              img.addEventListener( 'load', incrementCounter, false );
        } );
    
        function incrementCounter() {
            counter++;
            if ( counter === len ) {
                // All images loaded, go ahead.
                var $grid = $('.grid').isotope();
            }
        }
    });
    

提交回复
热议问题