小程序:滑塊視圖swiper

代碼GitHub地址

一.基本實(shí)現(xiàn)

<swiper>
  <swiper-item></swiper-item>
</swiper>
  • swiper: 父標(biāo)簽。其中只可放置<swiper-item/>組件,否則會(huì)導(dǎo)致未定義的行為。默認(rèn)width:100%;height:150px;
  • swiper-item: 子標(biāo)簽。 僅可放置在<swiper/>組件中,寬高自動(dòng)設(shè)置為100%。

二. 屬性說明

屬性名 類型 默認(rèn)值 說明
vertical Boolean false 滑動(dòng)方向是否為縱向
indicator-dots Boolean false 是否顯示面板指示點(diǎn)。橫向顯示:指示點(diǎn)在低中部??v向顯示:指示點(diǎn)在右中部。
indicator-color Color rgba(0, 0, 0, .3) 未選中的指示點(diǎn)顏色
indicator-active-color Color #000000 選中的指示點(diǎn)顏色
autoplay Boolean false 是否自動(dòng)輪播
interval Number 5000ms 自動(dòng)輪播的時(shí)間間隔
duration Number 500ms 輪播的動(dòng)畫時(shí)長
circular Boolean false 是否銜接循環(huán)滑動(dòng)
previous-margin String "0px" 前邊距,可用于露出前一項(xiàng)的一小部分,接受 px 和 rpx 值
next-margin String "0px" 后邊距,可用于露出后一項(xiàng)的一小部分,接受 px 和 rpx 值
display-multiple-items Number 1 同時(shí)顯示的swiper-item的數(shù)量
current Number 0 當(dāng)前所在滑塊的 index。 設(shè)置該屬性,可以讓swiper默認(rèn)顯示第幾個(gè)元素,注意不要數(shù)組越界。
current-item-id String "" 當(dāng)前所在滑塊的 item-id ,不能與 current 被同時(shí)指定. 需要指定current-item-id為某一個(gè)swiper-item的item-id,不要指定到一個(gè)不存在的item-id
事件名 類型 說明
bindanimationfinish EventHandle 輪播動(dòng)畫結(jié)束時(shí)會(huì)觸發(fā) animationfinish 事件。即監(jiān)聽滑動(dòng)到第幾個(gè)item的事件。
bindchange EventHandle current 改變時(shí)會(huì)觸發(fā) change 事件

兩者基本沒有什么區(qū)別。

四. 其他

點(diǎn)擊事件 : 點(diǎn)擊某一個(gè)swiper跳轉(zhuǎn)頁面

// wxml
<swiper catchtap="onItemClick">
  <swiper-item>
    <view data-postId="10"></view>
  </swiper-item>
</swiper>
// js
  onItemClick:function(event) {
// 獲得到點(diǎn)擊的swiper-item進(jìn)行頁面跳轉(zhuǎn)處理,這里用模態(tài)展示效果
    wx.showToast({
      icon: "none",
      title: "postid=" + event.target.dataset.postid,
    })
  }

五. 可運(yùn)行代碼

  • wxml
<view class="container">
  <swiper class="swiper" indicator-dots autoplay circular indicator-color='gray' indicator-active-color='red' interval="3000" bindchange="bindchange" bindanimationfinish="animationfinish" catchtap="onItemClick" current-item-id="{{index}}">
    <swiper-item item-id="0">
      <view class="swiper-item swiper-item-zero" data-postId="0">第0個(gè)</view>
    </swiper-item>
    <swiper-item item-id="1">
      <view class="swiper-item swiper-item-one" data-postId="1">第1頁</view>
    </swiper-item>
    <swiper-item item-id="2">
      <view class="swiper-item swiper-item-two" data-postId="2">第2頁</view>
    </swiper-item>
  </swiper>
</view>
  • wxss
.swiper {
  background-color: red;
  width: 50%;
  height: 200px;
}

.swiper-item {
  text-align: center;
  line-height: 200px;
}

.swiper-item-zero {
  background-color: yellow;
}

.swiper-item-one {
  background-color: orange;
}

.swiper-item-two {
  background-color: greenyellow;
}
  • js
Page({
  data: {
    index: 1    // 默認(rèn)滑動(dòng)到第幾頁
  },

  onLoad: function (options) {
  },

  bindchange: function (event) {
    console.log("bindchange");
    console.log(event);
  },
  onItemClick: function (event) {
    wx.showToast({
      icon: "none",
      title: "postid=" + event.target.dataset.postid,
    })
  },
  animationfinish: function (event) {
    console.log("animationfinish");
    console.log(event);
  }
})
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容