基於javascript實現移動端輪播圖效果

本文實例為大傢分享瞭js實現移動端輪播圖效果的具體代碼,供大傢參考,具體內容如下

插件使用:

1.zepto.js
2.touch.js

實現效果

html部分:

<!-- 結構 -->
  <!-- li*6>a[href=#]>img[src=./images/$.jpg] -->
  <div class="box">
    <ul>
      <!-- 為瞭無縫連接,我們在圖片這裡前後增加瞭2個圖片 -->
      <li>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/6.jpg" alt=""></a>
      </li>
      <li>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/1.jpg" alt=""></a>
      </li>
      <li>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/2.jpg" alt=""></a>
      </li>
      <li>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/3.jpg" alt=""></a>
      </li>
      <li>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/4.jpg" alt=""></a>
      </li>
      <li>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/5.jpg" alt=""></a>
      </li>
      <li>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/6.jpg" alt=""></a>
      </li>
      <li>
        <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/1.jpg" alt=""></a>
      </li>
    </ul>
</div>

css部分:

 * {
      margin: 0;
      padding: 0;
    }
    
    ul {
      list-style: none;
    }
    
    .box {
      width: 100%;
      overflow: hidden;
    }
    
    ul {
      /* 把li變成8張之後,需要,把ul的寬度變寬 */
      width: 800%;
      /* transition: all 1s; */
    }
    
    li {
      float: left;
      width: 12.5%;
    }
    
    img {
      width: 100%;
    }

js部分:

//獲取DOM
  var box = $(".box");
  var img = $("ul img");
  var ul = $("ul");
  var imgWidth = img.width();

  var index = 1;
  var right = index * imgWidth;
  ul.css("transform", `translateX(-${right}px)`);
  setTimeout(function() {
    ul.css("transition", "all 500ms");
  }, 100);

  //往左滑,坐標在增大
  box.on("swipeLeft", function() {
    index++;
    // if (index == img.length) {
    //   index = 0;
    // }
    var left = index * imgWidth;
    ul.css("transform", `translateX(-${left}px)`);
  });
  //往左滑,坐標在增大
  box.on("swipeRight", function() {
    index--;
    // if (index == -1) {
    //   index = img.length - 1;
    // }
    var right = index * imgWidth;
    ul.css("transform", `translateX(-${right}px)`);
  });

  //過渡結束之後時進行
  ul.on("transitionend", function() {
    //1.判讀 index
    //往左面滑(滑倒倒數第一張的時候,其實顯示的已經是用戶想看的第一張)
    if (index == img.length - 1) {

      index = 1;
      //index修改完畢之後需要重新執行一遍
      var right = index * imgWidth;
      ul.css("transform", `translateX(-${right}px)`);

      //2.取消過渡效果
      ul.css("transition", "none");
      //3.已經判斷完畢瞭,重新打開過渡效果
      //這裡設置一個1毫秒的延遲,否則會一起進行
      setTimeout(function() {
        ul.css("transition", "all 500ms");
      }, 1);
    }

    //1.判讀 index
    //往右面滑(滑倒index為0的時候,顯示的是客戶想看的第6張圖)
    if (index == 0) {
      //2.取消過渡效果
      ul.css("transition", "none");
      index = img.length - 2;
      //index修改完畢之後需要重新執行一遍
      var left = index * imgWidth;
      ul.css("transform", `translateX(-${left}px)`);
      //延遲開啟過渡效果
      setTimeout(function() {
        ul.css("transition", "all 500ms");
      }, 1)
    }
  })

代碼目前就分享到這裡,歡迎大傢有問題積極評論。

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: