JavaScript 动态加载时不触发事件

标签 javascript jquery element swiper.js

我正在使用 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');
    });

With this error

有什么想法吗?谢谢!

编辑:

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

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

最佳答案

这没有帮助吗?

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');
                }
            }
        });
    }
};

关于JavaScript 动态加载时不触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58523568/

相关文章:

javascript - vis.js 在盒子节点内添加一个小圆圈

javascript - jQuery 没有选择 Bootstrap 按钮

jquery - CSS transition 在被隐藏后短暂地导致元素为 "hoverable"

javascript - PhantomJS 在弹出窗口中执行 JavaScript 以进行数据提取

javascript - 如何区分常规对象和 jquery 对象(页面元素)?

父类中的 jquery 索引

javascript - 在 Jquery Mobile 中刷新页面时如何使用 Coldfusion toScript?

javascript - 如何在div中附加多行模板字符串?

JavaScript 更改行中的元素并在不同的行中打印它们

javascript - 如何修复 iOS 10.3.1 中 Safari 的 html margin bottom 错误