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

前端 未结 5 1932
梦毁少年i
梦毁少年i 2021-02-09 16:18

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:12

    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.

提交回复
热议问题