首頁 > web前端 > js教程 > 主體

zepto中使用swipe.js製作輪播圖附swipeUp,swipeDown不起效果問題_javascript技巧

WBOY
發布: 2016-05-16 15:42:01
原創
2041 人瀏覽過

在行動web開發中,由於手機介面較小,為了能展示更多的圖片經常使用輪播圖並且還需要考慮到手機流量的問題,透過請教他人以及百度,個人感覺swipe.js比較好用。

它是一個純javascript工具,不需要跟其它js庫一起導入,同時兼容jQuery和zepto,壓縮版的大小只有6kb很適合移動端的開發,它的git地址:https://github .com/thebird/swipe

在git上對其的使用方式介紹的相當清楚,關鍵程式碼如下

<div id='slider' class='swipe'>
 <div class='swipe-wrap'>
  <div></div>
  <div></div>
  <div></div>
 </div>
</div>
.swipe {
 overflow: hidden;
 visibility: hidden;
 position: relative;
}
.swipe-wrap {
 overflow: hidden;
 position: relative;
}
.swipe-wrap > div {
 float:left;
 width:100%;
 position: relative;
}
登入後複製
window.mySwipe = Swipe(document.getElementById('slider'),opt);
登入後複製

其中.swipe嵌套.swipe-wrap這個html樹模型最好不要改動,至於最裡面的div可以更換其他,如li 等

僅只需要上訴的幾段程式碼即可完成輪播圖的製作,但是在實際的專案中,特別是在首頁頂部的banner上還需要加入page的索引,還需要對控制項的參數進行配置,它的主要參數配置如下:

startSlide Integer (default:0) - 開始捲動的位置

speed Integer (default:300) - 動畫捲動的間隔(秒數)

auto Integer - 開始自動幻燈片(以毫秒為單位幻燈片之間的時間)

continuous Boolean (default:true) - 創建一個無限的循環(當滑動到所有動畫結束時是否循環滑動)

disableScroll Boolean (default:false) - 捲動捲軸是否停止投影片捲動 

stopPropagation Boolean (default:false) - 是否停止事件冒泡 

callback Function - 幻燈片運行中的回呼函數

transitionEnd Function - 動畫運行結束的回呼函數 

而他的主要api函數如下:

prev():上一頁 

next():下一頁

getPos():取得目前頁面的索引 

getNumSlides():取得所有項的數量

slide(index, duration):滑動方法

以下是偶在專案中的實際運用的程式碼

<div class="banner">
      <div id="slider" class="swipe">
        <ul class="swipe-wrap">
          <li>
            <a href="javascript:void(0)">
              <img src="img/1.jpg">
            </a>
          </li>
          <li>
            <a href="javascript:void(0)">
              <img src="img/2.jpg">
            </a>
          </li>
          <li>
            <a href="javascript:void(0)">
              <img src="img/3.jpg">
            </a>
          </li>
          <li>
            <a href="javascript:void(0)">
              <img src="img/4.jpg">
            </a>
          </li>
          <li>
            <a href="javascript:void(0)">
              <img src="img/5.jpg">
            </a>
          </li>
        </ul>
        <ul class="slide-trigger">
          <li class="cur"></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
        </ul>
      </div>
    </div>
.banner {
        width: 100%;
        position: relative;
      }
      .banner .swipe {
        overflow: hidden;
        position: relative;
      }
      .banner .swipe-wrap {
        overflow: hidden;
        position: relative;
        list-style: none;
      }
      .banner .swipe-wrap li {
        float: left;
        position: relative;
      }
      .banner img {
        width: 100%;
        vertical-align: middle;
      }
      .banner .slide-trigger {
        position: absolute;
        left: 0;
        bottom: 0;
        width: 100%;
        z-index: 10;
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        -webkit-box-pack: center;
        -moz-box-pack: center;
        -ms-flex-pack: center;
        -webkit-justify-content: center;
        justify-content: center;
        list-style: none;
      }
      .banner .slide-trigger li {
        width: 10px;
        height: 10px;
        background: #FFF;
        margin: 5px;
        -webkit-border-radius: 50%;
        -moz-border-radius: 50%;
        -ms-border-radius: 50%;
        -o-border-radius: 50%;
        border-radius: 50%;
      }
      .banner .slide-trigger .cur {
        background: #2fc7c9;
      }
var slider = $('#slider');
    slider.find(".slide-trigger").find("li").eq(0).addClass("cur");
    window.mySwipe = new Swipe(document.getElementById('slider'), {
      speed: 400,
      auto: 3000,
      callback: function(index, elem) {
        slider.find(".slide-trigger").find("li").eq(index).addClass("cur").siblings().removeClass("cur");
      }
    });
登入後複製

zepto中的swipeUp,swipeDown不起效果

我正在看zepto,然後看到裡面一些事件的時候發現一個問題:

$(‘body').swipeUp(function(e){
alert(‘swipeUp');//偶尔有效
})
$(‘body').swipeDown(function(e){
alert(‘swipeDown');//偶尔有效
})
$(‘body').tap(function(){
alert(‘tap');//ok
})
$(‘body').swipeLeft(function(){
alert(‘swipeLeft');//ok
})
$(‘body').swipeRight(function(){
alert(‘swipeRight');//ok
})
登入後複製

在行動端swipeUp,swipeDown不起效果,另外幾個有效,又是怎麼回事呢?

解決方案一:

zepto要引入 touch.js模組 官網上是沒有的 去github下載 然後引入 touch.js即可

解二:

是因為阻止了瀏覽器預設的下拉事件,加上下面一段程式碼。

document.addEventListener('touchmove', function (event) {
event.preventDefault();
}, false);
登入後複製

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!