1. 程式人生 > >swiper實現觸控滑動

swiper實現觸控滑動

引入需要的檔案       

        <link href="css/swiper.min.css" rel="stylesheet" type="text/css">
        <script type="text/javascript" src="js/jquery-1.11.2.js"></script>
        <script type="text/javascript" src="js/swiper.min.js"></script>

使用

<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>

<!-- 如果需要分頁器 -->

<div class="swiper-pagination"></div>

<!-- 如果需要導航按鈕 -->

<div

class="swiper-button-prev"></div>

<div class="swiper-button-next"></div>

<!-- 如果需要滾動條 -->

<div class="swiper-scrollbar"></div>

</div>

<script>
    var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        paginationClickable: true,
//        direction: 'vertical',  設定成vertical可以控制螢幕上下滑動,預設的是左右滑動
//        speed:1000,  
    });
</script>

如果頁面中使用的有css3動畫,要在每次翻頁時載入動畫,可以將動畫寫成這種樣式

@keyframes slideInLeft {
  0% {
    -webkit-transform: translateX(-100%);
    -ms-transform: translateX(-100%);
    transform: translateX(-100%);
    visibility: visible;
  }


  100% {
    -webkit-transform: translateX(0);
    -ms-transform: translateX(0);
    transform: translateX(0);
  }
}

這兒加上.swiper-slide-active div
.swiper-slide-active div.slideInLeft {
  -webkit-animation-name: slideInLeft;
  animation-name: slideInLeft;
}

效果如下: