requireJS with JQuery, Masonry, & ImagesLoaded: Object [object Object] has no method 'imagesLoaded'

前端 未结 5 2204
春和景丽
春和景丽 2021-02-09 16:19

RequireJS newbie here. Trying to convert some JQuery code I had working fine in the old way to work w/ RequireJS.

Header of my page loads three JS files via script tags

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-09 17:14

    In our particular case we had RequireJS included and configured in our main template, but wanted to include Masonry/ImagesLoaded on one particular page.

    We kept our code in the template:

    
    
    

    Then we included a JavaScript file after this, on the page that used Masonry, that triggered Masonry/ImagesLoaded:

    requirejs(['jquery', 'https://unpkg.com/masonry-layout@4.2.2/dist/masonry.pkgd.min.js', 'https://unpkg.com/imagesloaded@4.1.4/imagesloaded.pkgd.min.js'],
        function ($, Masonry, ImagesLoaded) {
            $(document).ready(function () {
                ImagesLoaded('.js-masonry', function () {
                    new Masonry('.js-masonry', {
                        // This was required for us, but from what I can tell shouldn't need to be/may need to be updated per-usage.
                        itemSelector: '.item'
                    });
                });
            });
        }
    );
    

    I'm sure this could be further optimized, but it resolved the errors and didn't require any new packages to be included.

提交回复
热议问题