swiper:swiper的第二个实例未初始化

时间:2020-02-02 22:43:19

标签: javascript html swiper swiperjs

在我的网站上,我无法获得第二个swiper实例进行初始化。 第一台Swiper可以正常工作。第二个只是显示第一张幻灯片。

我已经尝试过将第二个实例包装成一个超时,就像某些人似乎建议的那样。我还尝试过更改变量名称(如代码所示)。

我在做什么错?感谢您的帮助!

代码如下:

<script>

    const swiper1 = new Swiper('.s1', {
      cssMode: true,
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
      pagination: {
        el: '.swiper-pagination'
      },
      mousewheel: true,
      keyboard: true,
    });

    const swiper2 = new Swiper('.s2', {
      cssMode: true,
      navigation: {
        nextEl: '.next-s2',
        prevEl: '.prev-s2',
      },
      pagination: {
        el: '.pagination-s2'
      },
      mousewheel: true,
      keyboard: true,
    });
  </script>
.swiperSection {
    width: 80%;
    text-align: center;
    margin: auto;
}

.swiper-container {
    width: 100%;
    height: 100%;
}

.swiper-slide {
    text-align: center;
    font-size: 18px;
    background: #fff;
    width: 100%;

    /* Center slide text vertically */
    display: -webkit-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: block;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    -webkit-justify-content: center;
    justify-content: justify;
    -webkit-box-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
    padding-bottom: 1em
}

.swiper-slide p {
    display: block;
}

.swiper-slide a {
    color: #df1e25;
    text-decoration: none;
    cursor: pointer;
}

.quotes {
    background-color: #000;
    color: #fff;
    text-align: center;
}

.quote-sign {
    font-size: 7em;
    font-family: serif;
    color: #df1e25;
    height: 110px;
    margin-top: 1em;
}

.swiper-slide-black {
    background-color: #000;
}

.quotes p {
    font-size: 1em;
    font-style: italic;
}

.quotes h6 {
    color: #df1e25
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://unpkg.com/swiper/css/swiper.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/swiper/js/swiper.min.js"></script>


<div class="content">
        <h1>[...]</h1>
        <div class="swiperSection">
            <div class="swiper-container s1">
                <div class="swiper-wrapper" >
                    <div class="swiper-slide">
                        [...]
                    </div>
                    <div class="swiper-slide">
                        [...]
                    </div>
                    <div class="swiper-slide">
                        [...]
                    </div>
                    <div class="swiper-slide">
                        [...]
                    </div>
                </div>
                <!-- Add Arrows -->
                <div class="swiper-button-next swiper-button-black"></div>
                <div class="swiper-button-prev swiper-button-black"></div>
                <!-- Add Pagination -->
                <div class="swiper-pagination swiper-pagination-black"></div>
            </div>
        </div>
    </div>

    <div class="quotes">
        <div class="quote-sign">“</div>
        <div class="swiperSection">
            <div class="swiper-container s2">
                <div class="swiper-wrapper" >
                    <div class="swiper-slide swiper-slide-black">
                        [...]
                    </div>
                    <div class="swiper-slide swiper-slide-black">
                        [...]
                    </div>
                    <div class="swiper-slide swiper-slide-black">
                        [...]
                    </div>
                    <div class="swiper-slide swiper-slide-black">
                        [...]
                    </div>
                </div>
                <!-- Add Arrows -->
                <div class="swiper-button-next swiper-button-white nex1-s2"></div>
                <div class="swiper-button-prev swiper-button-white prev-s2"></div>
                <!-- Add Pagination -->
                <div class="swiper-pagination swiper-pagination-white pagination-s2"></div>
            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:2)

我让它工作了(如下)。我认为答案是您在第二个“下一个”按钮的标记中有错字。当前为nex1-s2,应为next-s2,以匹配您设置第二张幻灯片的JavaScript中的内容。很容易错过,我只有在弄乱了一段时间代码后才抓到它。

    const swiper1 = new Swiper('#s1', {
      cssMode: true,
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
      pagination: {
        el: '.swiper-pagination'
      },
      mousewheel: true,
      keyboard: true,
    });

    const swiper2 = new Swiper('#s2', {
      cssMode: true,
      navigation: {
        nextEl: '.next-s2',
        prevEl: '.prev-s2',
      },
      pagination: {
        el: '.pagination-s2'
      },
      mousewheel: true,
      keyboard: true,
    });
.swiperSection {
    width: 80%;
    text-align: center;
    margin: auto;
}

.swiper-container {
    width: 100%;
    height: 100%;
}

.swiper-slide {
    text-align: center;
    font-size: 18px;
    background: #fff;
    width: 100%;

    /* Center slide text vertically */
    display: -webkit-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: block;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    -webkit-justify-content: center;
    justify-content: justify;
    -webkit-box-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
    padding-bottom: 1em
}

.swiper-slide p {
    display: block;
}

.swiper-slide a {
    color: #df1e25;
    text-decoration: none;
    cursor: pointer;
}

.quotes {
    background-color: #000;
    color: #fff;
    text-align: center;
}

.quote-sign {
    font-size: 7em;
    font-family: serif;
    color: #df1e25;
    height: 110px;
    margin-top: 1em;
}

.swiper-slide-black {
    background-color: #000;
}

.quotes p {
    font-size: 1em;
    font-style: italic;
}

.quotes h6 {
    color: #df1e25
}
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://unpkg.com/swiper/css/swiper.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/swiper/js/swiper.min.js"></script>

<body>
    <div class="content">
        <h1>[...]</h1>
        <div class="swiperSection">
            <div class="swiper-container" id="s1">
                <div class="swiper-wrapper" >
                    <div class="swiper-slide">
                        [aaa]
                    </div>
                    <div class="swiper-slide">
                        [bbb]
                    </div>
                    <div class="swiper-slide">
                        [ccc]
                    </div>
                    <div class="swiper-slide">
                        [ddd]
                    </div>
                </div>
                <!-- Add Arrows -->
                <div class="swiper-button-next swiper-button-black"></div>
                <div class="swiper-button-prev swiper-button-black"></div>
                <!-- Add Pagination -->
                <div class="swiper-pagination swiper-pagination-black"></div>
            </div>
        </div>
    </div>

    <div class="quotes">
        <div class="quote-sign">“</div>
        <div class="swiperSection">
            <div class="swiper-container" id="s2">
                <div class="swiper-wrapper">
                    <div class="swiper-slide swiper-slide-black">
                        [eee]
                    </div>
                    <div class="swiper-slide swiper-slide-black">
                        [fff]
                    </div>
                    <div class="swiper-slide swiper-slide-black">
                        [ggg]
                    </div>
                    <div class="swiper-slide swiper-slide-black">
                        [hhh]
                    </div>
                </div>
                <!-- Add Arrows -->
                <div class="swiper-button-next swiper-button-white next-s2"></div>
                <div class="swiper-button-prev swiper-button-white prev-s2"></div>
                <!-- Add Pagination -->
                <div class="swiper-pagination swiper-pagination-white pagination-s2"></div>
            </div>
        </div>
    </div>
</body>
</html>