微信小程序里一种简单的 Tab 可滑动切换方式的实现及代码

why 392 2024-08-05

本篇文章给大家带来的内容是关于微信小程序实现一个简单的Tab可滑动的切换方式(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

 最近一直在做小程序项目,对于不同需求来说真是烦不胜烦,之前做的订单页来说只需要可点击切换就可以,但是在后期的迭代中提到要求可滑动切换,下面我自己整理了一套比较简单暴力的滑动切换方式,与大家共享一下,下面有效果图。(菜鸟上路,不喜勿喷):

.wxml 

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

<!--pages/mine/order/order.wxml-->

<view class=&#39;order&#39;>

  <view class="swiper-tab">

    <view class="swiper-tab-item {{currentTab==0?&#39;active&#39;:&#39;&#39;}}" data-current="0" bindtap="clickTab">代付款</view>

    <view class="swiper-tab-item {{currentTab==1?&#39;active&#39;:&#39;&#39;}}" data-current="1" bindtap="clickTab">代发货</view>

    <view class="swiper-tab-item {{currentTab==2?&#39;active&#39;:&#39;&#39;}}" data-current="2" bindtap="clickTab">待收货</view>

    <view class="swiper-tab-item {{currentTab==3?&#39;active&#39;:&#39;&#39;}}" data-current="3" bindtap="clickTab">待评价</view>

    <view class="swiper-tab-item {{currentTab==4?&#39;active&#39;:&#39;&#39;}}" data-current="4" bindtap="clickTab">退款/售后</view>

  </view>

  <swiper current="{{currentTab}}" duration="300" bindchange="swiperTab">

    <swiper-item>

      <view>代付款</view>

    </swiper-item>

    <swiper-item>

      <view>代发货</view>

    </swiper-item>

    <swiper-item>

      <view>待收货</view>

    </swiper-item>

    <swiper-item>

      <view>待评价</view>

    </swiper-item>

    <swiper-item>

      <view>退款/售后</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

/* pages/mine/order/order.wxss */

.swiper-tab {

    width: 100%;

    border-bottom: 2rpx solid #ccc;

    text-align: center;

    height: 88rpx;

    line-height: 88rpx;

    display: flex;

    flex-flow: row;

    justify-content: space-between;

}

 

.swiper-tab-item {

    width: 30%;

    color: #434343;

    font-size: 28rpx;

}

 

.active {

    color: #f65959;

    border-bottom: 4rpx solid #f65959;

}

 

swiper {

    text-align: center;

    background-color: #fff

}

.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

// pages/mine/order/order.js

Page({

 

  /**

   * 页面的初始数据

   */

  data: {

    currentTab: 0

  },

 

  /**

   * 生命周期函数--监听页面加载

   */

  onLoad: function (options) {

   

  },

  //滑动切换

  swiperTab: function (e) {

    this.setData({

      currentTab: e.detail.current

    });

  },

 

  //点击切换

  clickTab: function (e) {

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

      return false;

    } else {

      this.setData({

        currentTab: e.target.dataset.current

      })

    }

  }

   

})

效果图:

在wxml部分其实可以用个wx:for 来做。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序中实现随机验证码的实例及代码呈现
下一篇:微信小程序中验证码倒计时计数的实例及代码解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~