Home  >  Article  >  Web Front-end  >  How to use Swiper to implement pagination

How to use Swiper to implement pagination

亚连
亚连Original
2018-06-14 17:07:544998browse

This article mainly introduces the use of Swiper custom paging in detail, which has certain reference value. Interested friends can refer to

Swiper custom paging for your reference. , the specific content is as follows

Final implementation renderings:

HTML DEMO (official website example)

<link rel="stylesheet" href="path/to/swiper.min.css">

<p class="swiper-container">
 <p class="swiper-wrapper">
  <p class="swiper-slide">Slide 1</p>
  <p class="swiper-slide">Slide 2</p>
  <p class="swiper-slide">Slide 3</p>
 </p>
 <!-- 如果需要分页器 -->
 <p class="swiper-pagination"></p>

 <!-- 如果需要导航按钮 -->
 <p class="swiper-button-prev"></p>
 <p class="swiper-button-next"></p>

 <!-- 如果需要滚动条 -->
 <p class="swiper-scrollbar"></p>
</p>

<script src="path/to/jquery.js"></script>
<script src="path/to/swiper.jquery.min.js"></script>

1. Set navigation buttons

Note: Sweiper's navigation buttons and some other DOM structures can be placed outside ".swiper-container".

<input class="btn btn-gray button-prev" name="" type="button" value="上一题">
<input class="btn btn-gray button-next" name="" type="button" value="下一题">

As long as the class of the button corresponds, it will be OK.

<script>  
 var mySwiper = new Swiper (&#39;.swiper-container&#39;, {
  // 如果需要前进后退按钮
  nextButton: &#39;.button-next&#39;,//对应"下一题"按钮的class
  prevButton: &#39;.button-prev&#39;,//对应"上一题"按钮的class
  pagination: &#39;.pagination&#39;,//分页容器的css选择器
  paginationType : &#39;custom&#39;,//自定义-分页器样式类型(前提)
  //设置自定义分页器的内容
  paginationCustomRender: function (swiper, current, total) {
   var _html = &#39;&#39;;
   for (var i = 1; i <= total; i++) {
    if (current == i) {
    _html += &#39;<li class="swiper-pagination-custom active">&#39; + i + &#39;</li>&#39;;
    }else{
    _html += &#39;<li class="swiper-pagination-custom">&#39; + i + &#39;</li>&#39;;
    }
   }
   return _html;//返回所有的页码html
  }
 })
 //给每个页码绑定跳转的事件 
 $(&#39;.swiper-pagination&#39;).on(&#39;click&#39;,&#39;li&#39;,function(){
  var index = this.innerHTML;
  mySwiper.slideTo(index-1, 500, false);//切换到第一个slide,速度为1秒
 })

</script>

2. Set up a custom paginator (see configuration above)

1.pagination: '.pagination' gives the pagination a container, css class name selector
2.paginationType: 'custom' Set the customization of the pagination
3.paginationCustomRender:function(){} Set the content of the custom pagination
4. Bind each page number to jump to the corresponding page number The events

# are all compiled by me. I hope it will be helpful to you in the future.

Related articles:

Use selenium to capture Taobao data information

##How to use Baidu Map to implement map grid

Comparison and distinction between Express and Koa2 in nodejs (detailed tutorial)

The above is the detailed content of How to use Swiper to implement pagination. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn