css 更改滑块中的数字格式分页

jjjwad0x  于 2023-03-05  发布在  其他
关注(0)|答案(4)|浏览(131)

有一个滑动滑块,现在分页看起来像1 - 3,我需要像01 - 03。
这里是一个演示
https://codepen.io/anakin-skywalker94/pen/RmWxbE
超文本标记语言

<!-- Slider main container -->
<div class="swiper-container">
    <!-- Additional required wrapper -->
    <div class="swiper-wrapper">
        <!-- Slides -->
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
    </div>
    <!-- If we need pagination -->
    <div class="swiper-pagination"></div>
</div>

JS系统

var swiper = new Swiper('.swiper-container', { 
      pagination: {
        el: '.swiper-pagination',
        type: 'fraction',
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
        direction: 'horizontal',
        loop: true,
        slidesPerView: 1,
        spaceBetween: 0,
        mousewheel: true,
        pagination: {
          el: '.swiper-pagination',
          type: 'fraction',
          clickable: true,

        renderFraction: function (currentClass, totalClass) {
        return '<span class="' + currentClass + '"></span>' + ' <span>-</span> ' + '<span class="' + totalClass + '"></span>'; }
        },

    });

谢谢!

pb3s4cty

pb3s4cty1#

您可以使用类似('0' + 4).slice(-2)的代码,以便将0添加到小于10且大于0的数字前面。
所以你的JS看起来像这样:

var swiper = new Swiper('.swiper-container', { 
  pagination: {
    el: '.swiper-pagination',
    type: 'custom',
    renderCustom: function (swiper, current, total) {
      return ('0' + current).slice(-2) + ' of ' + ('0' + total).slice(-2);
    }
  },
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },
  direction: 'horizontal',
  loop: true,
  slidesPerView: 1,
  spaceBetween: 0,
  mousewheel: true,
    renderCustom: function (swiper, current, total) {
      return current + ' of ' + total;
  }
});

请参见codepen上的工作演示:https://codepen.io/Orlandster/pen/jobZmz

z5btuh9x

z5btuh9x2#

由于其他答案不再起作用,这里是一个工作变体,具有某种简化的结构和最新的刷卡器版本:

var swiper = new Swiper('.swiper-container', {
    effect: 'fade',
    pagination: {
        el: '.swiper-pagination',
        type: 'fraction',
        formatFractionCurrent: function (number) {
            return ('0' + number).slice(-2);
        },
        formatFractionTotal: function (number) {
            return ('0' + number).slice(-2);
        },
        renderFraction: function (currentClass, totalClass) {
            return '<span class="' + currentClass + '"></span>' +
                   ' - ' +
                   '<span class="' + totalClass + '"></span>';
        }
    },
});
.swiper-container {
    width: 100%;
    height: 100px;
}

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

    /* 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;
}
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
<link href="https://unpkg.com/swiper/swiper-bundle.min.css" rel="stylesheet"/>

<!-- Swiper -->
<div class="swiper-container">
  <div class="swiper-wrapper">
    <div class="swiper-slide">Slide 1</div>
    <div class="swiper-slide">Slide 2</div>
    <div class="swiper-slide">Slide 3</div>
    <div class="swiper-slide">Slide 4</div>
    <div class="swiper-slide">Slide 5</div>
    <div class="swiper-slide">Slide 6</div>
    <div class="swiper-slide">Slide 7</div>
    <div class="swiper-slide">Slide 8</div>
    <div class="swiper-slide">Slide 9</div>
    <div class="swiper-slide">Slide 10</div>
  </div>
  <!-- Add Pagination -->
  <div class="swiper-pagination"></div>
</div>
yjghlzjz

yjghlzjz3#

var swiper = new Swiper('.swiper-container', { 
      pagination: {
        el: '.swiper-pagination',
        type: 'fraction',
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
        direction: 'horizontal',
        loop: true,
        slidesPerView: 1,
        spaceBetween: 0,
        mousewheel: true,
        pagination: {
          el: '.swiper-pagination',
          type: 'custom',
          clickable: true,
        renderCustom: function (swiper, current, total) {
          function numberAppend(d) {
    return (d < 10) ? '0' + d.toString() : d.toString();
}
      return numberAppend(current) + ' of ' + numberAppend(total); 
  }
        },

    });
.swiper-slide {
  width: 100%;
  height: 400px;
  background: yellow;
}
.swiper-container {
height:300px
}
<link href="https://idangero.us/swiper/dist/css/swiper.min.css" rel="stylesheet"/>
<script src="https://idangero.us/swiper/dist/js/swiper.min.js"></script>
<!-- Slider main container -->
<div class="swiper-container">
    <!-- Additional required wrapper -->
    <div class="swiper-wrapper">
        <!-- Slides -->
        <div class="swiper-slide">Slide 01</div>
        <div class="swiper-slide">Slide 02</div>
        <div class="swiper-slide">Slide 03</div>
    </div>
    <!-- If we need pagination -->
    <div class="swiper-pagination"></div>
</div>
6l7fqoea

6l7fqoea4#

滑动器9.0.1

pagination: {
    el: '.swiper-pagination',
    type: 'bullets',
    clickable: true,
    renderBullet: function (index, className) {
        let pageNumber = index + 1;
        if (pageNumber < 10) {
            pageNumber = '0' + pageNumber;
        }
        return '<span class="' + className + '">' + pageNumber + '</span>';
    }
},

相关问题