【问题标题】:SwiperJS - Previous and Next buttons not workingSwiperJS - 上一个和下一个按钮不起作用
【发布时间】:2020-09-28 22:51:44
【问题描述】:

我正在使用 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 部分有关,但不确定要尝试什么。

【问题讨论】:

    标签: javascript jquery swiper swiperjs


    【解决方案1】:

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

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

    这是由于 swiper 位于隐藏模式中。

    【讨论】:

    • 谢谢!这只是节省了我的一天
    【解决方案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',
      }
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        我们只需要在 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',
                  },
                });
        

        这对我有用

        【讨论】:

          猜你喜欢
          • 2013-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-13
          • 1970-01-01
          • 1970-01-01
          • 2017-05-26
          • 1970-01-01
          相关资源
          最近更新 更多