JavaScript 动态加载时不触发事件

JavaScript not triggering events when dynamically loaded

我正在使用 Swiper.js 库,但在通过 JavaScript.

动态加载元素时触发 'slideChange' 事件时遇到问题

这里是我的水平和垂直滑动器的初始化位置:

var swiper = {
    initialize : function() {
        swiperH = new Swiper('.swiper-container-h', {
            slidesPerView: 1,
            preloadImages: false,
            updateOnImagesReady: true,
            lazy: true,
        })
        .on('slideChange', function () {
            console.log('Swiped Horizonally');
        });

        swiperV = new Swiper('.swiper-container-v', {
            direction: 'vertical',
            slidesPerView: 1,
            preloadImages: false,
            updateOnImagesReady: true,
            lazy: true,
            effect: 'fade',
            loop: true,
            fadeEffect: {
                crossFade: true
            },
            pagination: {
                el: '.swiper-pagination-v',
                clickable: true,
            },
        })
        .on('slideChange', function () {
            console.log('Swiped Vertically');
        });
    }
};

水平的'slideChange'触发的原因是因为它已经在html文件中:

   <!-- Swiper -->
    <div class="dash-container">
       <div class="swiper-container swiper-container-h">
      <div class="swiper-wrapper" id="swipeData">

       </div>
        </div>
    </div>

现在,垂直幻灯片正在通过 JavaScript 加载,这就是垂直幻灯片 'slideChange' 不会触发的地方。

function loadDresses(selectedDresses) {
    return new Promise((resolve, reject) => {
        $('#swipeData').html('');

        for (var i = 0; i < selectedDresses.length; i++) {
            var vScroll = '<div class="swiper-slide"><div class="swiper-container swiper-container-v"><div class="swiper-wrapper" style="height: 100%;">';

            for (var j = 0; j < selectedDresses[i].images.length; j++) {
                vScroll += '<div class="swiper-slide"><img src="' + selectedDresses[i].images[j] + '"/></div>';
            }

            vScroll += '<div class="swiper-slide" style="display:table;height:100%;width:100%;">';
            vScroll += '</div></div></div><div class="swiper-pagination swiper-pagination-v">';

            $('#swipeData').append(vScroll).trigger('create');
        }

        resolve(true);
    });
}

错误发生在这个片段:

    .on('slideChange', function () {
        console.log('Swiped Vertically');
    });

有什么想法吗?谢谢!

编辑:

我尝试了以下方法来阻止它过早初始化,但仍然没有成功:

          loadDresses(dresses).then(function(result) {
            var t = setInterval(() => {
                swiper.initialize();
                clearInterval(t);
            }, 5000);
        });

这里有一些选项:

要保持​​应用程序的流程,您可以销毁滑块并重新初始化它。

swiperH.destroy()

然后

loadDresses();
swiper.initialize();

您每次手动更改幻灯片时都可以 mySwiper.update()

问题是在初始化过程中找不到带有 class swiper-pagination-v 的元素。

您可以为 class swiper-pagination-v 存在设置条件。

var swiper = {
    initialize : function() {
        swiperH = new Swiper('.swiper-container-h', {
            slidesPerView: 1,
            preloadImages: false,
            updateOnImagesReady: true,
            lazy: true,
        })
            .on('slideChange', function () {
                console.log('Swiped Horizonally');
            });

        if ($('.swiper-container-v').length > 0) {
            swiperV = new Swiper('.swiper-container-v', {
                direction: 'vertical',
                slidesPerView: 1,
                preloadImages: false,
                updateOnImagesReady: true,
                lazy: true,
                effect: 'fade',
                loop: true,
                fadeEffect: {
                    crossFade: true
                },
                pagination: {
                    el: '.swiper-pagination-v',
                    clickable: true,
                },
            })
                .on('slideChange', function () {
                    console.log('Swiped Vertically');
                });
        }
    }
};

这没有帮助吗?

var swiper = {
    initialize : function() {
        swiperH = new Swiper('.swiper-container-h', {
            slidesPerView: 1,
            preloadImages: false,
            updateOnImagesReady: true,
            lazy: true,
        })
        .on('slideChange', function () {
            console.log('Swiped Horizonally');
        });

        swiperV = new Swiper('.swiper-container-v', {
            direction: 'vertical',
            slidesPerView: 1,
            preloadImages: false,
            updateOnImagesReady: true,
            lazy: true,
            effect: 'fade',
            loop: true,
            fadeEffect: {
                crossFade: true
            },
            pagination: {
                el: '.swiper-pagination-v',
                clickable: true,
            },
            on: {
                slideChange: function() {
                    console.log('Swiped Vertically');
                }
            }
        });
    }
};