Using masonry with imagesloaded

前端 未结 3 932
清酒与你
清酒与你 2020-12-03 05:42

I\'m a js newbie and hope this questions doesn\'t seem too stupid.

I\'m using masonry for my site - works fine. I wanted to let my boxes appear just when masonry fin

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 06:36

    Install

    npm install masonry-layout --save
    
    npm install imagesloaded --save
    

    Then vanilla js options would be

    'use strict'
    
    const Masonry = require('masonry-layout')
    const imgloaded = require('imagesloaded')
    const elem = document.querySelector('.grid')
    var imgLoad = imgloaded( elem )
        function onAlways() {
            const msnry = new Masonry( elem, {
                // options
                columnWidth: '.grid-sizer',
                // do not use .grid-sizer in layout
                itemSelector: '.grid-item',
                percentPosition: true,
                gutter: 10
            })
        // console.log('all images are loaded')
    }
    if (elem) {
        // bind with .on()
        imgLoad.on( 'always', onAlways )
        // unbind with .off()
        // imgLoad.off( 'always', onAlways )
    }
    

    Then check the console for all images are loaded.

提交回复
热议问题