1. 程式人生 > >微信小程式模板-分頁滑動欄

微信小程式模板-分頁滑動欄

功能:
1.分頁欄與滑動檢視繫結
2.點選分頁欄自動滑動到對應檢視
3.滑動的到檢視對應分頁欄自動顯示選中樣式
效果圖
這裡寫圖片描述

上程式碼
wxml

<view class="tapNav">
  <view class="{{tabArr.tabCurrentIndex==0?'active':''}}" data-index="0" bindtap="veHandle">分頁標籤1</view>
  <view class="{{tabArr.tabCurrentIndex==1?'active':''}}"  data-index="1" bindtap
="veHandle">
分頁標籤2</view> <view class="
{{tabArr.tabCurrentIndex==2?'active':''}}" data-index="2" bindtap="veHandle">分頁標籤3</view> </view> <swiper id="swiper" indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" current
="
{{current}}" bindchange="swiperChange"> <block wx:for="{{imgUrls}}"> <swiper-item id="swiper-item"> <image id="imgae" src="{{item}}" class="slide-image" width="355" height="150"/> </swiper-item> </block> </swiper>

wxss

/*
1.橫向排列分頁標籤
2.每個分頁標籤各佔1/3
*/
.tapNav { display: flex; flex-direction: row; } .tapNav view{ flex:1; width:200rpx; height:100rpx; text-align: center; line-height: 100rpx; font-family: "微軟雅黑"; } /*選中樣式*/ .tapNav .active { color:blue; border-bottom:4rpx solid mediumseagreen; } #swiper { margin-top:40rpx; } #swiper image{ width:100%; }

js

//index.js
//獲取應用例項
var app = getApp()
Page({
  data: {
    // 圖片地址
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    //是否顯示面板指示點
    indicatorDots: true,
    //自動播放
    autoplay: true,
    //切換時間間隔
    interval: 2000,
    //滑動時長
    duration: 1000,
    //存放滑動檢視的current
    current:0,
    //分頁標籤class條件判斷的值
    tabArr:{
      tabCurrentIndex:0
    }
  },
  //事件處理函式
  //觸控分頁標籤觸發事件
  veHandle:function(e){
    //每個分頁標籤都設定了data-index,觸控觸發事件獲取此數值
    //用此數值替換滑動檢視的current
    //用此數值替換分頁標籤class判斷的值
    console.log(e.target.dataset.index)
    var currentIndex = e.target.dataset.index
    this.setData({
      current:currentIndex,
      "tabArr.tabCurrentIndex":currentIndex
    })
  },
  //通過滑塊檢視的current改變觸發事件
  swiperChange:function(e){
    //獲取檢視滑塊當前的current
    //用此數值替換分頁標籤的current的值
    console.log(e.detail.current)
    var swiperCurrent = e.detail.current;
    this.setData({
      'tabArr.tabCurrentIndex':swiperCurrent
    })
  },
  onLoad: function () {
    console.log('onLoad')
  }
})