微信小程序+vant组件 侧边导航栏切换显示

设计页面时希望效果:左侧侧边导航栏,右侧内容。点击左侧导航栏的不同块,右侧显示不同内容。

采用了vant组件中侧边导航栏van-sidebar,van-sidebar子标签包括多个van-sidebar-item

实现方法:

van-sidebar中设置 bind:change="onChange":

        <van-sidebar  active-key="{{ activeKey }}" bind:change="onChange">
                <van-sidebar-item title="农家水果" custom-class="cellTab" />
                <van-sidebar-item title="农家蔬菜" custom-class="cellTab"/>
                <van-sidebar-item title="自种茶叶" custom-class="cellTab"/>
                <van-sidebar-item title="加工零食" custom-class="cellTab"/>
                <van-sidebar-item title="加工饮料" custom-class="cellTab"/>
                <van-sidebar-item title="加工调料" custom-class="cellTab"/>
       </van-sidebar>


随后在js文件中编写onChange(event),其中event.detail即对应导航栏块的索引。以event.detail作switch参数,在case中通过this.setData设置右侧区域内容。
 

onChange(event) {
    switch(event.detail){
      case 0:
        this.setData({
          goods:[
            {
              price:"5.6元/斤",
              tag:"热销",
              title:"农家大橘子",
              image:"../myImage/orange.webp",
              value:"5"
            },
            {
              price:"6.6元/斤",
              tag:"新品",
              title:"农家大香蕉",
              image:"../myImage/banana.webp",
              value:"4"
            }
          ]
        });
        break;
        case 1:
          this.setData({
            goods:[
              {
                price:"5.6元/斤",
                tag:"热销",
                title:"农家白菜",
                image:"../myImage/vegetable.webp",
                value:"5"
              },
              {
                price:"6.6元/斤",
                tag:"新品",
                title:"农家西红柿",
                image:"../myImage/tomato.webp",
                value:"4"
              }
            ]
          })
    }
  }

即可实现跳转效果

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
微信小程序中使用 Vant 组件库的侧边导航栏滚动,需要首先在项目中引入 Vant 组件库,并且按照 Vant 的文档进行组件的配置和使用。 首先,在项目根目录下的 `app.json` 文件中配置 Vant 组件的全局样式,示例如下: ```json { "style": "vant-weapp/dist/common/index" } ``` 然后在需要使用侧边导航栏滚动的页面中引入 Vant侧边导航栏组件,并按照需求设置好相关属性和事件,示例如下: ```html <van-sidebar show="{{ true }}" width="200rpx" :style="height: 100vh;"> <van-sidebar-item v-for="(item, index) in menuList" :key="index" :title="item.title" :dot="item.dot" :badge="item.badge" /> </van-sidebar> ``` 其中,`show` 属性用于控制侧边导航栏显示与隐藏,`width` 属性用于设置导航栏的宽度,`:style` 属性用于设置导航栏的高度为可视区域的高度。 此外,需要根据实际需求在 `data` 数据中定义 `menuList` 数组,并为每个侧边导航栏项设置相应的标题、角标和小红点。 最后,在页面的生命周期函数中,通过监听滚动事件来实现侧边导航栏的滚动效果,示例如下: ```javascript Page({ onScroll: function(event) { // 根据滚动位置处理侧边导航栏的样式 } }) ``` 在监听滚动事件中,可以根据滚动位置来处理侧边导航栏的样式,例如高亮当前正在滚动的导航项。 总结起来,要在微信小程序中使用 Vant 组件库的侧边导航栏滚动,需要首先引入 Vant 组件库并进行配置,然后在页面中按照需求引入侧边导航栏组件并设置相应的属性和事件,最后在监听滚动事件中处理导航栏的样式即可。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值