最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to limit the displayed number of bullets swiperjs - Stack Overflow

programmeradmin3浏览0评论

Is it possible in swiperjs to make it so that 5 pagination buttons of the same size are always indicated. When switching, if there are more than 5 slides, then the first button disappeared and the next one appeared at the end (button 6). DynamicBullets behavior: true, together with custom pagination it is very strange.Or maybe there is some other ready-made slider that has something similar. I ask for help

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=".min.css">
<script src=".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>

Is it possible in swiperjs to make it so that 5 pagination buttons of the same size are always indicated. When switching, if there are more than 5 slides, then the first button disappeared and the next one appeared at the end (button 6). DynamicBullets behavior: true, together with custom pagination it is very strange.Or maybe there is some other ready-made slider that has something similar. I ask for help

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./swiper/swiper-bundle.min.css">
<script src="https://unpkg./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>

Share Improve this question asked Feb 12, 2021 at 7:39 qwertyqwerty 2351 gold badge2 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

add these lines:

dynamicBullets: true,

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
    }
}
发布评论

评论列表(0)

  1. 暂无评论