2

我正在使用 SwiperJS (www.swiperjs.com),我遇到了滑动到下一张照片的问题,但上一个和下一个按钮没有。我已经查看了我可以在这里找到的每一篇文章以及遵循他们的文档,但仍然没有任何运气。

    <div id="openModal" class="modalDialog" style="display: none;">
    <div class="modal-content">
        <a href="#close" title="Close" class="close" onclick="$('#openModal').hide()">X</a>
        <h2>Modal Box</h2>
        <p>Hello world</p>
        <script src="https://unpkg.com/swiper/js/swiper.min.js"></script>
        <link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.css">
        <link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css">
        <!-- Slider main container -->
        <div class="swiper-container">
            <!-- Additional required wrapper -->
            <div id="swiper" class="swiper-wrapper">
                <!-- Slides -->
                <div class="swiper-slide"><img src="http://<server>/path/to/image.png"></div>
                <div class="swiper-slide"><img src="http://<server>/path/to/image2.png"></div>
            </div>
            <!-- If we need pagination -->
            <div class="swiper-pagination"></div>

            <!-- If we need navigation buttons -->
            <div class="swiper-button-prev"></div>
            <div class="swiper-button-next"></div>

            <!-- If we need scrollbar -->
            <div class="swiper-scrollbar"></div>
        </div>
        <script>
        var mySwiper = new Swiper ('.swiper-container', {
            // Optional parameters
            direction: 'horizontal',
            loop: true,
            centeredSlides: true,
            slidesPerView: 1,

            pagination: '.swiper-pagination',
            paginationClickable: '.swiper-pagination',
            nextButton: '.swiper-button-next',
            prevButton: '.swiper-button-prev',
        })
        </script>

为了初始化 Swiper JS,我也从他们的网站上尝试过:

  <script>
  var mySwiper = new Swiper ('.swiper-container', {
    // Optional parameters
    direction: 'vertical',
    loop: true,

    // If we need pagination
    pagination: {
      el: '.swiper-pagination',
    },

    // Navigation arrows
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },

    // And if we need scrollbar
    scrollbar: {
      el: '.swiper-scrollbar',
    },
  })
  </script>

有人对我可以尝试什么有任何想法吗?我还发现分页按钮没有像他们的示例那样显示在底部,但与上一个和下一个按钮不起作用相比,这不是一个问题。就像我说的,如果我单击并拖动或“滑动”,它会很好地移动到下一张幻灯片,所以我假设它与 JS 部分有关,但不确定要尝试什么。

4

4 回答 4

4

谢谢您的帮助!我的问题的问题最终是我需要将其添加到我的 swiper 配置中:

            observer: true,
            observeParents: true,
            parallax:true,

这是因为 swiper 在隐藏的模态中。

于 2020-06-11T17:49:51.233 回答
2

在您发布的第一部分代码中,您必须更改 JS

<script>
  var mySwiper = new Swiper ('.swiper-container', {
      // Optional parameters
      direction: 'horizontal',
      loop: true,
      centeredSlides: true,
      slidesPerView: 1,

      pagination: {
        el: '.swiper-pagination',
        clickable: true,
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      }
  })
  </script>

箭头和分页都应该起作用。

我改变的是

pagination: '.swiper-pagination',
paginationClickable: '.swiper-pagination',


 pagination: {
        el: '.swiper-pagination',
        clickable: true,
      },



nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev',


navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  }
于 2020-06-10T13:45:51.803 回答
1

以这种方式连接滑块,一切都会好起来的!

import { Swiper, Parallax, Navigation} from 'swiper'
Swiper.use([ Parallax, Navigation ])

document.addEventListener('DOMContentLoaded', () => {
  const swiper = new Swiper('.swiper-container', {
    // Optional parameters
    direction: 'horizontal',
    loop: false,
    speed: 2400,
    parallax: true,
    loop: false,

    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },
  });
})
于 2021-08-25T07:16:08.740 回答
0

只是我们需要在 swiper js 配置中添加以下内容

observer: true,
observeParents: true,
parallax:true,

所以这里是完整的配置

var swiper = new Swiper('.relatedproducts', {
            
              observer: true,
            observeParents: true,
            parallax:true,
          slidesPerView: 6,
          loop:true,
          autoplay: true,
          simulateTouch:true,
          navigation: {
            nextEl: '.relatedproducts .swiper-button-next',
            prevEl: '.relatedproducts .swiper-button-prev',
          },
        });

这对我有用

于 2021-06-03T13:05:38.847 回答