小程序组件导航切换

项目中新建一个 components文件夹;鼠标选中components文件夹右键选择新建component,输入Tabs组件名会自动生成Tabs.js\Tabs.json\Tabs.wxml\Tabs.wxss文件。

组件Tabs文件夹如下:

Tabs.wxml //文件

<!-- 导航 -->
<view class="tabbar">
  <view wx:for="{{tabList}}" wx:key="{{index}}" bindtap="switchTab" data-index="{{index}}" class="tab-item {{currentTab === index ? 'active' : ''}}">
    {{item}}
  </view>
</view>

<!-- 导航内容 -->
<view class="content">
  <view wx:for="{{tabContentList}}" wx:key="{{index}}" wx:if="{{currentTab === index}}" class="tab-content">
    {{item}}
  </view>
</view>

Tabs.js  //文件

Component({
  // 组件的属性列表
  properties: {

  },
  //  组件的初始数据
  data: {
    currentTab: 0,
    tabList: ['Tab1', 'Tab2', 'Tab3','Tab4', 'Tab5'],
    tabContentList: ['Tab 1 Content', 'Tab 2 Content', 'Tab 3 Content','Tab 4 Content', 'Tab 5 Content'],
  },

  //  组件的方法列表
  methods: {
    switchTab(event) {
      const index = event.currentTarget.dataset.index;
      this.setData({
        currentTab: index,
      });
    },
  }
})

Tabs.wxss  //文件

.tabbar {
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: #fff;
  box-shadow: 0px -2px 4px rgba(0, 0, 0, 0.1);
  padding-bottom: 10px;
}

.tab-item {
  font-size: 16px;
  color: #333;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  padding-bottom: 4px;
}

.tab-item.active {
  color: #ff0000;
  border-bottom-color: #ff0000;
  background-color: transparent; /* 将背景色设置为透明 */
}

.content {
  margin-top: 20px;
  text-align: center;
  font-size: 18px;
}

.tab-content {
  padding: 20px;
}


Tabs.json //文件

{
  "component": true,
  "usingComponents": {}
}

 页面demo文件夹如下:

demo.wxml  //文件

<Tabs>
    <view>
        <tabbar></tabbar>
    </view>
</Tabs>

demo.json  //文件

{
  "navigationBarTextStyle": "white",
  "navigationBarBackgroundColor": "#005e29",
  "usingComponents": {
    "Tabs":"/components/Tabs/Tabs"  //引入组件
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值