微信小程序—自定义嵌套导航(图文)

微信小程序—自定义嵌套导航

效果如下:
在这里插入图片描述
前情须知:在classify页面里引入组件

第一步:在page文件夹新建classify的文件夹和page
在这里插入图片描述
wxml

<view class="container">
  <view class="nav">
    <view 
      class="tab-item {{currentTab == 0 ? 'active' : ''}}" 
      data-current="0" 
      bindtap='switchTab'
    >
      <text class='item-text'>自营</text>
    </view>
    <view 
      class="tab-item {{currentTab == 1 ? 'active' : ''}}" 
      data-current="1" 
      bindtap='switchTab'
    >
      <text class='item-text'>严选</text>
    </view>
  </view>
  <view class="content">
    <view wx:if="{{currentTab == 0}}"><ziying/></view>
    <view wx:if="{{currentTab == 1}}"><yanxuan/></view>
  </view>
</view>

js

Page({
  /**
   * 页面的初始数据
   * Linyufan.com
   */
  data: {
    currentTab: 0
  },
  switchTab(e) {
    this.setData({ currentTab: e.currentTarget.dataset.current });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  onShow: function () {
   
  }
})

wxss

.nav{
  display: flex;
  justify-content: space-between;
}
.nav .tab-item{
  flex: 1;
  text-align: center;
}
.active{
  color: #1eb1bb;
  border-bottom: 2px solid #1eb1bb;
}

第二步:新建component文件夹专门用来存放组件并新建ziying和yanxuan文件夹和component
在这里插入图片描述
注意是组件不是页面
这时一级导航已经可以点击了
这时

第三步:二级导航
在component文件夹新建jingdian和nvshen和banshou文件夹和component
在这里插入图片描述
这三个都是ziying的二级导航

第四步:ziying文件的代码
wxml:

<view class="container">
  <view class="tab-wrapper">
    <view 
      class="tab-item {{currentTab == 0 ? 'active' : ''}}" 
      bindtap='switchTab' 
      data-current="0"
    >经典</view>
    <view 
      class="tab-item {{currentTab == 1 ? 'active' : ''}}" 
      bindtap='switchTab' 
      data-current="1"
    >女神</view>
    <view 
      class="tab-item {{currentTab == 2 ? 'active' : ''}}" 
      bindtap='switchTab' 
      data-current="2"
    >伴手礼</view>
  </view>
  <view class="content-wrapper" wx:if='{{currentTab == 0}}'><jingdian/></view>
  <view class="content-wrapper" wx:if='{{currentTab == 1}}'><nvshen/></view>
  <view class="content-wrapper" wx:if='{{currentTab == 2}}'><banshou/></view>
</view>

js

// component/ziying/ziying.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    currentTab: 0,
    tabItems: [
      {
        id: 0,
        title: '经典'
      },
      {
        id: 1,
        title: '女神'
      },
      {
        id: 2,
        title: '伴手礼'
      }
    ]
  },
  
  /**
   * 组件的方法列表
   */
  methods: {
    switchTab(e) {
      this.setData({ currentTab: e.currentTarget.dataset.current });
    }
  }
})

wxss

.tab-wrapper{
  display: flex;
  justify-content: space-between;
}
.tab-item{
  flex: 1;
  text-align: center;
  font-size: 28rpx;
}
.active{
  color: #1eb1bb;
  font-weight: bold;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值