小程序实现tab标签页:可点击切换,滑动切换页面

本文介绍了一个使用HTML、CSS和JavaScript实现的简易页面Tab切换效果。通过简单的布局和样式设置,可以轻松地为用户提供友好的标签页浏览体验。文章包含了完整的代码示例。

页面效果:

HTML: 


  <view wx:if="{{userType==0}}" style="height:100%">
    <view class="tab_box" style="width: 100%">
      <view class="tabs_v">
        <block wx:for="{{tabs}}" wx:for-item="item" wx:key="index">
          <view class="tab_v {{index<1?'tab_nature':'tab_course'}}" bindtap='changeCurrentTab_' data-index='{{index}}'
            data-current='{{index}}'>
            <text class="tab_txt {{currentTab==index? 'tab_v_active' : ''}}">{{item.label}}</text>
          </view>
        </block>
      </view>
    </view>
    <!-- tab 容器 -->
    <view class="tabWrap">
      <swiper class="" current="{{currentTab}}" duration="300" bindchange="swiperTab" style="width:100%;height:100%;">
        <!-- 页面1 -->
        <block>
          <swiper-item style="width:100%;height:100%;">
            <view class="" style="width:100%;height:100%;">
              <scroll-view style="width:100%;height:100%;" scroll-y="true" bindscrolltolower='scrollToLower'
                bindscrolltoupper='scrollToUpper' lower-threshold='30' upper-threshold='30'>
                <view style="width:100%;height:auto;">
                  页面1
                </view>
              </scroll-view>
            </view>
          </swiper-item>
        </block>
        <!-- 页面2 -->
        <block>
          <swiper-item style="width:100%;height:100%;">
            <view class="" style="width:100%;height:100%;">
              <scroll-view style="width:100%;height:100%;" scroll-y="true" bindscrolltolower='scrollToLower'
                bindscrolltoupper='scrollToUpper' lower-threshold='30' upper-threshold='30'>
                <view style="width:100%;height:auto;">
                  页面2
                </view>
              </scroll-view>
            </view>
          </swiper-item>
        </block>
      </swiper>
    </view>
  </view>

CSS:

/* tab */
.tab_box{
  height: 50px;
  display: flex;
  flex-direction: row;
  background-color: #fff;
  border-bottom: 1px solid #E5E5E5;
  margin-bottom: 15px;
}
.tabs_v{
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
.tab_v{
  width: 50%;
  text-align: center;
}
.tab_txt{
  border-bottom: 2px solid transparent;
  padding-bottom: 14px;
  color: #999999;
}
.tab_v_active{
  border-bottom: 2px solid #00C6AC;
  color: #00C6AC;
  font-weight: bold;
}
/* 容器 */
.tabWrap{
  width: 100%;
  height: calc(100% - 67px);
  position: relative;
  border-bottom: 1px solid #e6e6e6;
}

JS:


Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {label:'访客', index: 0},
      {label:'工作人员', index: 1}
    ],
    currentTab:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  // tab切换
  changeCurrentTab_(e){
    let that = this
    if (that.data.currentTab === e.currentTarget.dataset.current){
      return false
    }else{
      that.setData({ 
        currentTab: e.currentTarget.dataset.current
      })
    }
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  }
})

 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路痴不脸盲

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值