4.小程序scroll-view与swiper组合实现tab切换

最近接手一个需要tab切换的小程序,废话不多说,直接上代码

1、wxml
<view class="container">
    <!-- tab导航栏 -->
    <!-- scroll-left属性可以控制滚动条位置 -->
    <!-- scroll-with-animation滚动添加动画过渡 -->
    <scroll-view scroll-x="true" class="nav" scroll-left="{{navScrollLeft}}" scroll-with-animation="{{true}}">
        <block wx:for="{{navData}}" wx:for-index="idx" wx:for-item="navItem" wx:key="idx">
            <view class="nav-item {{currentTab == idx ?'active':''}}" style='width:{{tab_width}}%' data-current="{{idx}}" bindtap="switchNav">{{navItem.text}}                </view>
        </block>        
    </scroll-view>
    <!-- 页面内容 -->

    <swiper class="tab-box" current="{{currentTab}}" duration="100" bindchange="switchTab">        
        <swiper-item wx:for="{{contentDada}}" wx:for-item="tabItem" wx:for-index="idx" wx:key="idx" class="tab-content">
            {{tabItem.text}}
        </swiper-item>
    </swiper>
</view>
2、wxss
/**index.wxss**/
page{
    width: 100%;
    height: 100%;
}
.container{
    width: 100%;
    height: 100%;
}
.nav {
    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;
}
.nav-item {
    /* width: 33.3%; */
    display: inline-block;
    text-align: center;
}
.nav-item.active{
    color: red;
}
.tab-box{
    background: yellow;
    height: 100%;
    box-sizing: border-box;
    width: 100%;
}
.tab-content{
    overflow-y: scroll;
}
::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
}
3、js
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    navData: [
      {
        text: '1'
      },
      {
        text: '2'
      },
      {
        text: '3'
      },
      {
        text: '4'
      },
      {
        text: '5'
      },
      {
        text: '6'
      },
      {
        text: '7'
      },
      {
        text: '8'
      },
      {
        text: '9'
      }
    ],
    contentDada:[
      {
        text: '内容1'
      },
      {
        text: '内容2'
      },
      {
        text: '内容3'
      },
      {
        text: '内容4'
      },
      {
        text: '内容5'
      },
      {
        text: '内容6'
      },
      {
        text: '内容7'
      },
      {
        text: '内容8'
      },
      {
        text: '内容9'
      }
      ],
    currentTab: 0,
    navScrollLeft: 0,
    tab_width:0
  },
  //事件处理函数
  onLoad: function () {
    if(this.data.navData.length == 1){
      this.setData({
        tab_width : 100
      })
    } else if (this.data.navData.length == 2){
      this.setData({
        tab_width: 50
      })
    }else{
      this.setData({
        tab_width: 33.3
      })
    }
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      },
    })
    wx.request({
      url: '',
      data:{

      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        console.log(res.data)
      }
    })
  },
  // 顶部导航实现
  switchNav(event) {
    var cur = event.currentTarget.dataset.current;
    console.log(cur)
    //每个tab选项宽度占,最多显示3个tab
    var navDataLength = this.data.navData.length;
    if (this.data.navData.length >= 3){
      navDataLength = 3
    }
    var singleNavWidth = this.data.windowWidth / navDataLength;
    //tab选项居中                            
    this.setData({
      navScrollLeft: (cur - 2) * singleNavWidth
    })
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
  },
  // 内容区域实现
  switchTab(event) {
    var cur = event.detail.current;
    var navDataLength = this.data.navData.length;
    if (this.data.navData.length >= 3) {
      navDataLength = 3
    }
    console.log(cur)
    var singleNavWidth = this.data.windowWidth / navDataLength;
    this.setData({
      currentTab: cur,
      navScrollLeft: (cur - 1) * singleNavWidth
    });   
  }
})
4、效果图

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 `swiper` 组件中切换 `view` 时,可以通过设置 `v-if` 或 `v-show` 控制 `view` 的显示与隐藏。为了实现高度记忆功能,我们需要在 `view` 中设置一个自定义属性,例如 `height`,用于记录当前 `view` 的高度。然后在 `swiper` 的 `change` 事件中,获取当前 `view` 的高度并保存到对应的 `view` 中,这样在下次切换到该 `view` 时,可以通过 `height` 属性设置 `view` 的高度,从而实现高度记忆功能。 具体实现步骤如下: 1. 在 `view` 中设置一个自定义属性 `height`,用于记录当前 `view` 的高度,例如: ``` <view v-show="activeTab === 0" height="{{tabHeights[0]}}">...</view> <view v-show="activeTab === 1" height="{{tabHeights[1]}}">...</view> <view v-show="activeTab === 2" height="{{tabHeights[2]}}">...</view> ``` 2. 在 `swiper` 的 `change` 事件中,获取当前 `view` 的高度并保存到对应的 `view` 中,例如: ``` <swiper @change="handleChange"> <swiper-item> <view>...</view> </swiper-item> <swiper-item> <view>...</view> </swiper-item> <swiper-item> <view>...</view> </swiper-item> </swiper> ... data() { return { activeTab: 0, tabHeights: [0, 0, 0] }; }, methods: { handleChange(e) { const { current } = e.detail; const query = uni.createSelectorQuery().in(this); query.select(`#tab-${current}`).boundingClientRect((rect) => { this.tabHeights[current] = rect.height; }).exec(); this.activeTab = current; } } ``` 3. 在 `view` 中动态设置高度,例如: ``` <view v-show="activeTab === 0" :style="{ height: tabHeights[0] + 'px' }">...</view> <view v-show="activeTab === 1" :style="{ height: tabHeights[1] + 'px' }">...</view> <view v-show="activeTab === 2" :style="{ height: tabHeights[2] + 'px' }">...</view> ``` 这样就可以实现切换 `view` 时记忆高度的功能了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值