【问题标题】:How to limit the displayed number of bullets swiperjs如何限制显示的子弹数量 swiperjs
【发布时间】:2021-02-12 07:39:31
【问题描述】:

是否可以在 swiperjs 中使其始终显示 5 个相同大小的分页按钮。切换时,如果幻灯片超过 5 张,则第一个按钮消失,最后一个按钮出现(按钮 6)。 DynamicBullets behavior: true, 加上自定义分页很奇怪。或者也许还有其他一些现成的滑块有类似的东西。我寻求帮助

window.addEventListener('load', function() {

var menu = ['1989', '1990', '1991', '1992', '1993', '1994','1995']
var mySwiper = new Swiper ('.swiper-container', {
    // If we need pagination
    pagination: {
      el: '.swiper-pagination',
      clickable: true,
      dynamicBullets: true,
        renderBullet: function (index, className) {
          return '<span class="' + className + '">' + (menu[index]) + '</span>';
        },
    },

    // Navigation arrows
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },
  })
})
.swiper-container {
    width: 100%;
    height: 500px;
}
.swiper-slide {
    text-align: center;
    font-size: 18px;
    background: #fff;
    /* Center slide text vertically */
    display: -webkit-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    -webkit-justify-content: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
}
.swiper-pagination {
    position: absolute;
    top: 10px;
    right: 10px;
    width: auto !important;
    left: auto !important;
    margin: 0;
}
.swiper-pagination-bullet {
    padding: 5px 10px!important;
    border-radius: 0!important;
    width: auto!important;
    height: 30px!important;
    text-align: center!important;
    line-height: 30px!important;
    font-size: 12px!important;
    color:#000!important;
    opacity: 1!important;
    background: rgba(0,0,0,0.2)!important;
    
}
.swiper-pagination-bullet-active {
    color:#fff!important;
    background: #007aff!important;
<link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css">
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
<div class="swiper-container">
    <!-- Additional required wrapper -->
    <div class="swiper-wrapper">
        <!-- Slides -->
        <div class="swiper-slide">Content</div>
        <div class="swiper-slide" >Content</div>
        <div class="swiper-slide">Content</div>
              <div class="swiper-slide">Content</div>
              <div class="swiper-slide">Content</div>
              <div class="swiper-slide">Content</div>
              <div class="swiper-slide">Content</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>
</div>

【问题讨论】:

    标签: javascript swiperjs


    【解决方案1】:

    添加这些行:

    动态子弹:真,

    dynamicMainBullets:4

    var swiper = new Swiper ('.swiper-container', {
        direction: 'horizontal',
        observer: true,
        observeParents: true,
        slidesPerView: 1,
        centeredSlides: false,
        spaceBetween: 10,
            pagination: {
                el: ".swiper-pagination",
                dynamicBullets: true,
                dynamicMainBullets: 4
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-27
      • 2019-11-01
      • 2021-05-18
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 2012-01-02
      • 2020-12-09
      • 1970-01-01
      相关资源
      最近更新 更多