首頁 > 軟體

小程式實現頁面多級來回切換的範例程式碼

2022-07-07 10:00:19

第一步

首先通過swiper建立一個簡單的多tab頁面

通過觸發pagechange1方法中的事件對currentIndex來進行賦值,又通過currentIndex的改變使前端wxml對應更改,這個部分對滑動和點選的操作都一樣,無非就是使currentIndex對應到各自的位置,通過數位來決定位置

//滑動
  pagechange1: function (ee) {
    if ("touch" === ee.detail.source) {
      let currentPageIndex = this.data.currentIndex;
      currentPageIndex = (currentPageIndex + 1) % 2;
     
      this.setData({
        currentIndex: currentPageIndex,
      })
    }
  },
//點選tab時觸發
  titleClick: function (e) {
    this.setData({
      //拿到當前索引並動態改變
      currentIndex: e.currentTarget.dataset.idx
    })
  },

這個部分完整程式碼如下:

wxml

<view>
    <!-- Tab佈局 -->
    <view class='navBox'>
        <view class='titleBox' bindtap='titleClick' data-idx='0'>
            <text class="{{0 == currentIndex ? 'fontColorBox' : ''}}">安卓</text>
            <hr class="{{0 == currentIndex ? 'lineBox' : 'notLineBox'}}" />
        </view>
        <view class='titleBox' bindtap='titleClick' data-idx='1'>
            <text class="{{1 == currentIndex ? 'fontColorBox1' : ''}}">蘋果</text>
            <hr class="{{1 == currentIndex ? 'lineBox' : 'notLineBox'}} " />
        </view>
    </view>
    <!-- 內容佈局 -->
    <swiper class='swiperTtemBox' bindchange='pagechange1' current='{{currentIndex}}'>
        <swiper-item class='swiperTtemBox'>
            <view>內容1</view>
        </swiper-item>
        <swiper-item class='swiperTtemBox'>
            <view>內容2</view>
        </swiper-item>
    </swiper>
</view>

wxss

Page {
  /* 全域性樣式 */
  background: rgb(244, 245, 249);
  height: 100%;
  position: fixed;
}

.fontColorBox,
.fontColorBox1 {
  /* 文字預設顏色 */
  color: black;
}

.navBox {
  /* 頂部tab盒子樣式 */
  width: 100%;
  height: 108rpx;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

.navBox view:last-child {
  /* 最後一個tab標題的樣式 */
  padding-left: 20%;
}

.titleBox {
  /* 未選中文字的樣式 */
  color: rgb(168, 170, 175);
  font-size: 30rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.lineBox,.notLineBox{
  /* 選中及未選中底線共同樣式 */
  width: 32rpx;
  height: 8rpx;
}

.lineBox {
  /* 選中底線樣式 */
  background: rgb(43, 44, 45);
  margin-top: 16rpx;
  border-radius: 4rpx;
}

.notLineBox {
  /* 未選中底線樣式 */
  background: transparent;
}

.swiperTtemBox {
  /* 底部內容樣式 */
  height: 100vh;
  overflow: scroll;
  margin: 12rpx 0rpx;
  background: white;
  font-size: 28rpx;
}

js

const app = getApp()
Page({
  data: {
    currentIndex: 0, //預設第一個
  },
  pagechange1: function (ee) {
    if ("touch" === ee.detail.source) {
      let currentPageIndex = this.data.currentIndex;
      currentPageIndex = (currentPageIndex + 1) % 2;
     
      this.setData({
        currentIndex: currentPageIndex,
      })
    }
  },

  //點選tab時觸發
  titleClick: function (e) {
    this.setData({
      //拿到當前索引並動態改變
      currentIndex: e.currentTarget.dataset.idx
    })
  },
})

第二步

上一步完成後,下級頁面再加一個滑動頁面,當內切換結束後,在做切換就是父級的切換操作

在“內容1”的view中 寫入程式碼即可,由於父級程式碼只能是小於2個頁面才有效,所以我們不用父級的這個滑動來做子滑動,不僅僅是因為bug的問題,這樣也避免了樣式和資料重複的問題

在這裡我們插入wxml程式碼:

 <view >
    <scroll-view scroll-x="true" class="tab-h" scroll-left="{{scrollLeft}}">
        <view class="tab-item {{currentTab==0?'active':''}}"  data-current="0" bindtap="swichNav">熱門</view>
        <view class="tab-item {{currentTab==1?'active':''}}" data-current="1" bindtap="swichNav">影音</view>
        <view class="tab-item {{currentTab==2?'active':''}}" data-current="2" bindtap="swichNav">閱讀</view>
        <view class="tab-item {{currentTab==3?'active':''}}" data-current="3" bindtap="swichNav">遊戲</view>
        <view class="tab-item {{currentTab==4?'active':''}}" data-current="4" bindtap="swichNav">福利</view>
    </scroll-view>
    <swiper class="tab-content" current="{{currentTab}}" duration="300" bindchange="switchTab"
     style="height:{{winHeight}}rpx">
        <swiper-item>
            <scroll-view scroll-y="true" class="scoll-h" >
              <view class="item-ans">
                        <view class="avatar">
                            <image class="img" src="https://profile.csdnimg.cn/9/B/A/0_qq_35230125"></image>
                        </view>
                        <view class="expertInfo">
                            <view class="name">剪影安卓版</view>
                            <view class="tag">111人下載</view>
                            <view>這只是一個簡介,看的話點選詳情最大</view>
                        </view>
                        <view class="askBtn" bindtap="show_hideModal">下載</view>
                        <!-- <navigator url="/pages/askExpert/expertDetail" class="askBtn">問TA</navigator>  -->
                    </view>
            </scroll-view>
        </swiper-item>
        <swiper-item>
            <scroll-view scroll-y="true" class="scoll-h" >
              <view class="item-ans">
                        <view class="avatar">
                            <image class="img" src="http://ookzqad11.bkt.clouddn.com/avatar.png"></image>
                        </view>
                        <view class="expertInfo">
                            <view class="name">歡顏</view>
                            <view class="tag">知名情感博主</view>
                            <view class="answerHistory">134個回答,2234人聽過 </view>
                        </view>
                        <navigator url="/pages/askExpert/expertDetail" class="askBtn">問T2A</navigator> 
                    </view>
            </scroll-view>
        </swiper-item>
        <swiper-item>
            <scroll-view scroll-y="true" class="scoll-h" >
              <view class="item-ans">
                        <view class="avatar">
                            <image class="img" src="http://ookzqad11.bkt.clouddn.com/avatar.png"></image>
                        </view>
                        <view class="expertInfo">
                            <view class="name">歡顏</view>
                            <view class="tag">知名情感博主</view>
                            <view class="answerHistory">134個回答,2234人聽過 </view>
                        </view>
                        <navigator url="/pages/askExpert/expertDetail" class="askBtn">問T2A</navigator> 
                    </view>
            </scroll-view>
        </swiper-item>
        <swiper-item>
            <scroll-view scroll-y="true" class="scoll-h" >
              <view class="item-ans">
                        <view class="avatar">
                            <image class="img" src="http://ookzqad11.bkt.clouddn.com/avatar.png"></image>
                        </view>
                        <view class="expertInfo">
                            <view class="name">歡顏</view>
                            <view class="tag">知名情感博主</view>
                            <view class="answerHistory">134個回答,2234人聽過 </view>
                        </view>
                        <navigator url="/pages/askExpert/expertDetail" class="askBtn">問T2A</navigator> 
                    </view>
            </scroll-view>
        </swiper-item>
        <swiper-item>
            <scroll-view scroll-y="true" class="scoll-h" >
              <view class="item-ans">
                        <view class="avatar">
                            <image class="img" src="http://ookzqad11.bkt.clouddn.com/avatar.png"></image>
                        </view>
                        <view class="expertInfo">
                            <view class="name">歡顏</view>
                            <view class="tag">知名情感博主</view>
                            <view class="answerHistory">134個回答,2234人聽過 </view>
                        </view>
                        <navigator url="/pages/askExpert/expertDetail" class="askBtn">問T2A</navigator> 
                    </view>
            </scroll-view>
        </swiper-item>
       
    </swiper>
</view>

js加入:

data: {
    winHeight:"",//視窗高度
    currentTab:0, //預設當前項的值
    scrollLeft:0, //tab標題的卷軸位置
    currentIndex: 0, //預設是活動項 切換
    hideModal:false//遮罩層
  },

部分完整js程式碼:

// pages/leftSlide/leftSlide.js

const App = getApp()

Page({
  data: {
    winHeight:"",//視窗高度
    currentTab:0, //預設當前項的值
    scrollLeft:0, //tab標題的卷軸位置
    currentIndex: 0, //預設是活動項 切換
    hideModal:false//遮罩層
  },
      // 捲動切換標籤樣式
      switchTab:function(e){
        let that=this;
        that.setData({
            currentTab:e.detail.current
        });
        that.checkCor();
    },
    // 點選標題切換當前頁時改變樣式
    swichNav:function(e){
        var cur=e.target.dataset.current;
        console.log(cur);
        if(this.data.currentTaB==cur){return false;}
        else{
            this.setData({
                currentTab:cur
            })
        }
    },
    //判斷當前捲動超過一屏時,設定tab標題卷軸。
    checkCor:function(){
      if (this.data.currentTab>4){
        this.setData({
          scrollLeft:300
        })
      }else{
        this.setData({
          scrollLeft:0
        })
      }
    },
  pagechange: function (ee) {
    let that=this;
    console.log(ee.detail.source)
    if ("touch" === ee.detail.source) {
      let currentPageIndex = that.data.currentIndex;
      currentPageIndex = (currentPageIndex+1) % 2;
     
      that.setData({
        currentIndex: currentPageIndex,
      })
    }
  },
// 彈出、隱藏遮罩層

show_hideModal:function(){
  let that=this;
  that.setData({
    hideModal:true
  })

},
hideModal:function(){
  let that=this;
  that.setData({
    hideModal:false
  })
},
// 



  // 切換
  // 切換swiper-item觸發bindchange事件
  pagechange: function (e) {
    // 通過touch判斷,改變tab的下標值
    if ("touch" === e.detail.source) {
      let currentPageIndex = this.data.currentIndex;
      currentPageIndex = (currentPageIndex + 1) % 2;
      // 拿到當前索引並動態改變
      this.setData({
        currentIndex: currentPageIndex,
      })
    }
  },

  //點選tab時觸發
  titleClick: function (e) {
    this.setData({
      //拿到當前索引並動態改變
      currentIndex: e.currentTarget.dataset.idx
    })
  },



 
  onLoad: function (options) {
    // 頁面初始化 options為頁面跳轉所帶來的引數
    var that = this; 
        //  高度自適應
        wx.getSystemInfo( {  
            success: function( res ) {  
                var clientHeight=res.windowHeight,
                    clientWidth=res.windowWidth,
                    rpxR=750/clientWidth;
              var  calc=clientHeight*rpxR-180;
                console.log(calc)
                that.setData( {  
                    winHeight: calc  
                });  
            }  
        });
  },
  onReady: function () {
    // 頁面渲染完成
  },
  onShow: function () {
    // 頁面顯示
  },
  onHide: function () {
    // 頁面隱藏
  },
  onUnload: function () {
    // 頁面關閉
  }
})

樣式修改為下方的即可

/* 頁面切換 */
Page {
  /* 全域性樣式 */
  background: rgb(244, 245, 249);
  height: 100%;
  position: fixed;
}

.fontColorBox,

.fontColorBox1 {
  /* 文字預設顏色 */
  color: black;
}

.navBox {
  /* 頂部tab盒子樣式 */
  width: 100%;
  height: 80rpx;
  background: white;
  flex-direction: row;
  display: flex;
  align-items: center;
  justify-content: center;
}
  /* 最後一個tab標題的樣式 */
/* .navBox view:last-child {

  padding-left: 20%;
} */

.titleBox {
  width: 100rpx;
  /* 未選中文字的樣式 */
  color: rgb(168, 170, 175);
  font-size: 30rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.lineBox,.notLineBox{
  /* 選中及未選中底線共同樣式 */
  width: 32rpx;
  height: 8rpx;
}

.lineBox {
  /* 選中底線樣式 */
  background: rgb(43, 44, 45);
  margin-top: 16rpx;
  border-radius: 4rpx;
}

.notLineBox {
  /* 未選中底線樣式 */
  background: transparent;
}

.swiperTtemBox {
  /* 底部內容樣式 */
  height: 100vh;
  overflow: scroll;
  margin: 12rpx 0rpx;
  background: white;
  font-size: 28rpx;
}


/* 頁面切換 */



/* 遮罩層 */
/* pages/index/components/buy/index.wxss */
 
.flex {
  display: flex;
  align-items: center;
}
 
.box {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
}
 
.empty-box {
  flex: 1;
  background-color: transparent;
}
 
/* 內容檢視 */
 
.content {
  width: 100vw;
  background: rgba(255, 255, 255, 1);
  opacity: 1;
  border-radius: 20px 20px 0px 0px;
  z-index: 1001;
}

 

 
/* modal按鈕 */
 
.button {
  width: 100vw;
  padding: 4rpx 20rpx 10rpx 40rpx;
}
 
.button >view {
  width: calc(100% - 80rpx);
  height: 98rpx;
  border-radius: 50rpx;
  line-height: 98rpx;
  text-align: center;
  font-size: 32rpx;
  font-family: PingFang SC;
  font-weight: bold;
  color: rgba(255, 255, 255, 1);
  background: yellowgreen;
  opacity: 1;
}





/* 內部切換欄 */
.tab-h{
  height: 80rpx;width: 100%; box-sizing: border-box;overflow: hidden;line-height: 80rpx;background: #F7F7F7; font-size: 16px; white-space: nowrap;position: fixed;top: 0; left: 0; z-index: 99;}
.tab-item{margin:0 36rpx;display: inline-block;}
.tab-item.active{color: #4675F9;position: relative;}
.tab-item.active:after{ content: "";display: block;height: 8rpx;width: 52rpx;background: #4675F9;position: absolute; bottom: 0;left: 5rpx;border-radius: 16rpx;}
.item-ans{ width: 100%;display: flex; flex-grow: row no-wrap;justify-content: space-between; padding: 30rpx;box-sizing: border-box; height: 180rpx;align-items: center;border-bottom: 1px solid #F2F2F2;}
.avatar{width: 100rpx;height: 100rpx;position: relative;padding-right: 30rpx;}
.avatar .img{width: 100%;height: 100%;}
.avatar .doyen{width: 40rpx;height: 40rpx;position: absolute;bottom: -2px;right: 20rpx;}
.expertInfo{font-size: 12px;flex-grow: 2;color: #B0B0B0;line-height: 1.5em;}
.expertInfo .name{font-size: 16px;color:#000;margin-bottom: 6px;}
.askBtn{ width: 120rpx;height: 60rpx;line-height: 60rpx;text-align: center;font-size: 14px; border-radius: 60rpx;border: 1px solid #4675F9; color:#4675F9;}
.tab-content{margin-top: 80rpx;}
.scoll-h{height: 100%;}

全部完整程式碼

最後完整的程式碼如下:

wxml

<!-- 切換 -->
<view>
    <!-- Tab佈局 -->
    <view class='navBox'>
        <view class='titleBox' bindtap='titleClick' data-idx='0' style="width: 200rpx;">
            <text class="{{0 == currentIndex ? 'fontColorBox' : ''}}">安卓</text>
            <hr class="{{0 == currentIndex ? 'lineBox' : 'notLineBox'}}" />
        </view>
        <view class='titleBox' bindtap='titleClick' data-idx='1' style="width: 200rpx;">
            <text class="{{1 == currentIndex ? 'fontColorBox1' : ''}}">蘋果</text>
            <hr class="{{1 == currentIndex ? 'lineBox' : 'notLineBox'}} " />
        </view>
      
        
    </view>
    <!-- 內容佈局 -->
    <swiper class='swiperTtemBox' bindchange='pagechange' current='{{currentIndex}}'>
        <swiper-item class='swiperTtemBox'>
            <!-- 安卓 -->
            <view >
    <scroll-view scroll-x="true" class="tab-h" scroll-left="{{scrollLeft}}">
        <view class="tab-item {{currentTab==0?'active':''}}"  data-current="0" bindtap="swichNav">熱門</view>
        <view class="tab-item {{currentTab==1?'active':''}}" data-current="1" bindtap="swichNav">影音</view>
        <view class="tab-item {{currentTab==2?'active':''}}" data-current="2" bindtap="swichNav">閱讀</view>
        <view class="tab-item {{currentTab==3?'active':''}}" data-current="3" bindtap="swichNav">遊戲</view>
        <view class="tab-item {{currentTab==4?'active':''}}" data-current="4" bindtap="swichNav">福利</view>
    </scroll-view>
    <swiper class="tab-content" current="{{currentTab}}" duration="300" bindchange="switchTab"
     style="height:{{winHeight}}rpx">
        <swiper-item>
            <scroll-view scroll-y="true" class="scoll-h" >
              <view class="item-ans">
                        <view class="avatar">
                            <image class="img" src="https://profile.csdnimg.cn/9/B/A/0_qq_35230125"></image>
                        </view>
                        <view class="expertInfo">
                            <view class="name">剪影安卓版</view>
                            <view class="tag">111人下載</view>
                            <view>這只是一個簡介,看的話點選詳情最大</view>
                        </view>
                        <view class="askBtn" bindtap="show_hideModal">下載</view>
                        <!-- <navigator url="/pages/askExpert/expertDetail" class="askBtn">問TA</navigator>  -->
                    </view>
            </scroll-view>
        </swiper-item>
        <swiper-item>
            <scroll-view scroll-y="true" class="scoll-h" >
              <view class="item-ans">
                        <view class="avatar">
                            <image class="img" src="http://ookzqad11.bkt.clouddn.com/avatar.png"></image>
                        </view>
                        <view class="expertInfo">
                            <view class="name">歡顏</view>
                            <view class="tag">知名情感博主</view>
                            <view class="answerHistory">134個回答,2234人聽過 </view>
                        </view>
                        <navigator url="/pages/askExpert/expertDetail" class="askBtn">問T2A</navigator> 
                    </view>
            </scroll-view>
        </swiper-item>
        <swiper-item>
            <scroll-view scroll-y="true" class="scoll-h" >
              <view class="item-ans">
                        <view class="avatar">
                            <image class="img" src="http://ookzqad11.bkt.clouddn.com/avatar.png"></image>
                        </view>
                        <view class="expertInfo">
                            <view class="name">歡顏</view>
                            <view class="tag">知名情感博主</view>
                            <view class="answerHistory">134個回答,2234人聽過 </view>
                        </view>
                        <navigator url="/pages/askExpert/expertDetail" class="askBtn">問T2A</navigator> 
                    </view>
            </scroll-view>
        </swiper-item>
        <swiper-item>
            <scroll-view scroll-y="true" class="scoll-h" >
              <view class="item-ans">
                        <view class="avatar">
                            <image class="img" src="http://ookzqad11.bkt.clouddn.com/avatar.png"></image>
                        </view>
                        <view class="expertInfo">
                            <view class="name">歡顏</view>
                            <view class="tag">知名情感博主</view>
                            <view class="answerHistory">134個回答,2234人聽過 </view>
                        </view>
                        <navigator url="/pages/askExpert/expertDetail" class="askBtn">問T2A</navigator> 
                    </view>
            </scroll-view>
        </swiper-item>
        <swiper-item>
            <scroll-view scroll-y="true" class="scoll-h" >
              <view class="item-ans">
                        <view class="avatar">
                            <image class="img" src="http://ookzqad11.bkt.clouddn.com/avatar.png"></image>
                        </view>
                        <view class="expertInfo">
                            <view class="name">歡顏</view>
                            <view class="tag">知名情感博主</view>
                            <view class="answerHistory">134個回答,2234人聽過 </view>
                        </view>
                        <navigator url="/pages/askExpert/expertDetail" class="askBtn">問T2A</navigator> 
                    </view>
            </scroll-view>
        </swiper-item>
       
    </swiper>
</view>
            <!-- 安卓 -->
        </swiper-item>
        <swiper-item class='swiperTtemBox'>
            <view>活動內容</view>
        </swiper-item>
        
    </swiper>
</view>

<!-- 切換 -->

<!-- 彈窗 -->
<!--pages/index/components/buy/index.wxml-->
<view class="box" hidden="{{!hideModal}}">
  <view class="empty-box" bindtap="hideModal" id="empty-box"></view>
  <scroll-view scroll-y style="max-height:80vh;">
    <view class="content" style="transform:translateY({{translateY}}px);" animation="{{animate}}">
      <!-- boll -->
 <view style="height: 750rpx;display: flex;flex-direction: column;align-items: center;">
 <view style="height: 750rpx;width: 700rpx;display: flex;flex-direction: column;align-items: center;padding-top: 10rpx;">
 <!-- 圖示icon -->
 <view style="width: 100rpx;height: 100rpx;border-radius: 10rpx;">
 <image src="http://pic.2265.com/upload/2017-5/2017515111376293.png" style="width: 100rpx;height: 100rpx;"></image>
 </view>
 <view style="width: 730rpx;height: 500rpx;">
  <text decode="{{true}}" style="width: 730rpx;height: 400rpx;">&ensp;
    &ensp;&ensp;&ensp;&ensp;❤️❤️❤️❤️❤️❤️ 

IT145.com E-mail:sddin#qq.com