javascript - Swiper.js - 未捕获的类型错误 : Cannot read property 'params' of null

标签 javascript jquery

我正在使用 swiper.js 和一次 swiper1.destroy();运行、重建、滑动到某个幻灯片并单击。

我收到错误 swiper.js:438 Uncaught TypeError: Cannot read property 'params' of null

这是代码。我感谢所有的帮助。非常感谢。

$('.call').click(function(e){
e.preventDefault()

$("#menu").addClass("hide");
$("#slider").removeClass("hide");

selector.push("address");

var swiper1 = new Swiper('.swiper1', {
    pagination: '.one',
    paginationClickable: true,
    hashnav: true,
    loop:true,
    initialSlide:0
});

getLocation();

$('.noclick').click(function(e){
    e.preventDefault()
    swiper1.unlockSwipes(); // <-- This seems to be causing the problem
    swiper1.slidePrev(); // <-- This seems to be causing the problem
    player.seekTo(0);
})

$('.yes').click(function(e){
    e.preventDefault()
    swiper1.unlockSwipes(); // <-- This seems to be causing the problem
    swiper1.slideNext(); // <-- This seems to be causing the problem
})

$('.overlay').click(function(e){
    swiper1.unlockSwipes();
    console.log("overlay");
    e.preventDefault()
    $("#menu").removeClass("hide");
    $("#slider").addClass("hide");
    swiper1.destroy();
})

swiper1.on('slideChangeStart', function () {
    var dataindex = $(".swiper-slide-active").data('index');
    console.log(dataindex);

    if(dataindex == 8){
        onPlayerReady();
        swiper1.lockSwipes();
        setTimeout(function(){
            //var state = player.getPlayerState();
            //console.log(state);
            //if (state == 0){
                //alert("This should work");
                swiper1.unlockSwipes();
                swiper1.slideTo(9);
            //}
        },4000);

    }else if(dataindex == 9) {  
        swiper1.lockSwipes();
    }else if(dataindex == 10){
        swiper1.lockSwipes();
    }else{
        stopVideo();
    }
});

})

最佳答案

我遇到了类似的错误,我需要正确删除 swiper.destroy() 上的旧事件监听器并附加新的事件监听器,因为上一个事件监听器引用了被破坏的对象。

就我而言,它看起来像这样

// on init
window.addEventListener('keydown', this.handleKeypressFn)

..
// than somewhere before destroy()
window.removeEventListener('keydown', this.handleKeypressFn)
swiper1.destroy()

关于javascript - Swiper.js - 未捕获的类型错误 : Cannot read property 'params' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34625845/

相关文章:

javascript - jQuery:设置/重置具有特定类的表单字段(改进代码)

javascript - 正则表达式从字符串中提取电子邮件

javascript - 防止用户点击除 Textarea 之外的 Enter

javascript - 使用 jQuery 缩小文本搜索的选择范围

javascript - 如何使用 jQuery(或 CSS)淡入/淡出文本?

javascript - 如何在网页中制作一个无需重新加载即可编辑的列表

javascript - Angularjs Bootstrap 下拉菜单不起作用

javascript - knockout js - 单击 foreach 语句下的列表时如何更改列表颜色?

javascript - youtube video iframe onclick

javascript - 选择行在 IE 中不显示正确数量的选定行