微信小程序實例:實現頂部tab切換以及滑動切換時導航欄會隨着移動的效果(代碼)


本篇文章給大家帶來的內容是關於微信小程序實例:實現頂部tab切換以及滑動切換時導航欄會隨着移動的效果(代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

 

實現的效果:

js:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

Page({

    data: {

        // tab切換 

        currentTab: 0,

    },

    swichNav: function (e) {

        console.log(e);

        var that = this;

        if (this.data.currentTab === e.target.dataset.current) {

            return false;

        } else {

            that.setData({

                currentTab: e.target.dataset.current,

            })

        }

    },

    swiperChange: function (e) {

        console.log(e);

        this.setData({

            currentTab: e.detail.current,

        })

  

    },

    onLoad: function (options) {

        // 生命周期函數--監聽頁面加載

    },

    onReady: function () {

        // 生命周期函數--監聽頁面初次渲染完成

    },

    onShow: function () {

        // 生命周期函數--監聽頁面顯示

    },

    onHide: function () {

        // 生命周期函數--監聽頁面隱藏

    },

    onUnload: function () {

        // 生命周期函數--監聽頁面卸載

    },

    onPullDownRefresh: function () {

        // 頁面相關事件處理函數--監聽用戶下拉動作

    },

    onReachBottom: function () {

        // 頁面上拉觸底事件的處理函數

    },

    onShareAppMessage: function () {

        // 用戶點擊右上角分享

        return {

            title: 'title', // 分享標題

            desc: 'desc', // 分享描述

            path: 'path' // 分享路徑

        }

    }

})

 

  wxml:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<view class="page">

  

  <!--頂部導航欄-->

  <view class="swiper-tab">

    <view class="tab-item {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">Tab1</view>

    <view class="tab-item {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swichNav">Tab2</view>

    <view class="tab-item {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swichNav">Tab3</view>

  </view>

  

  <!--內容主體-->

  <swiper class="swiper" current="{{currentTab}}" duration="200" bindchange="swiperChange">

    <swiper-item>

      <view>我是tab1</view>

    </swiper-item>

    <swiper-item>

      <view>我是tab2</view>

    </swiper-item>

    <swiper-item>

      <view>我是tab3</view>

    </swiper-item>

  </swiper>

</view>

 

  wxss:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

.page {

  margin-left: 10rpx;

  margin-right: 10rpx;

}

  

.swiper-tab {

  display: flex;

  flex-direction: row;

  line-height: 80rpx;

  border-bottom: 2rpx solid #777;

}

  

.tab-item {

  width: 33.3%;

  text-align: center;

  font-size: 15px;

  color: #777;

}

  

.swiper {

  height: 1100px;

  background: #dfdfdf;

}

  

.on {

  color: blue;

  border-bottom: 5rpx solid blue;

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM