Skip to content

Sidebar 侧边分类

用于电商商品分类选择、多级导航联动的侧边垂直导航栏控件,支持双向滚动联动(点击左侧平滑定位右侧,滑动右侧自动高亮左侧)、指示条与数字徽章。


代码示例

1. 基础侧边导航 (Basic Sidebar)

vue
<template>
  <mu-sidebar
    v-model:active="active"
    :options="categories"
    width="200"
    @change="onChange"
  />
</template>

<script setup>
import { ref } from 'vue'

const active = ref(0)
const categories = ref([
  { name: '热门推荐', badge: 'HOT' },
  { name: '手机数码' },
  { name: '家用电器' }
])

const onChange = (item, index) => {
  console.log('切换侧边分类:', index, item)
}
</script>

2. 双向滚动联动电商分类 (Two-Way Scroll Linkage)

结合右侧 <scroll-view> 监听 @scroll 与指定 :scroll-into-view 实现左右双向实时滑动联动。

vue
<template>
  <view class="sidebar-layout">
    <!-- 左侧 Sidebar -->
    <mu-sidebar
      v-model:active="activeSidebar"
      :options="categories"
      width="200"
      @change="onSidebarClick"
    />

    <!-- 右侧 scroll-view -->
    <scroll-view
      scroll-y
      class="sidebar-scroll"
      :scroll-into-view="rightScrollIntoId"
      scroll-with-animation
      @scroll="onRightScroll"
    >
      <view
        v-for="(cat, index) in categories"
        :key="index"
        :id="'cat-section-' + index"
        class="cat-section"
      >
        <text class="category-title">{{ cat.name }}</text>
        <!-- 子分类内容列表 -->
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { ref } from 'vue'

const activeSidebar = ref(0)
const rightScrollIntoId = ref('')
let isClickScrolling = false

const categories = ref([
  { name: '热门推荐', badge: 'HOT' },
  { name: '手机数码' },
  { name: '家用电器' }
])

// 1. 点击左侧侧边栏 -> 平滑滚动右侧
const onSidebarClick = (item, index) => {
  isClickScrolling = true
  activeSidebar.value = index
  rightScrollIntoId.value = 'cat-section-' + index
  setTimeout(() => { isClickScrolling = false }, 500)
}

// 2. 滚动右侧 -> 自动更新左侧 Sidebar 高亮
const onRightScroll = (e) => {
  if (isClickScrolling) return
  // 计算当前滚动高度对应的分类索引...
}
</script>

API 属性说明 (Props)

参数名说明类型可选值默认值
v-model:active / active当前激活选中的分类索引Number-0
options分类数据源 (支持字符串数组或对象 [{name, badge, icon}])Array-[]
width侧边栏宽度 (rpx)Number | String-180
iconSize分类图标尺寸大小 (rpx)Number | String-36
iconPosition图标相对文字的位置String'left' | 'top''left'
activeColor激活态文字高亮颜色String-''
indicatorColor左侧高亮立柱指示条颜色String-'#171717'

事件说明 (Events)

事件名说明回调参数
update:activev-model:active 双向绑定更新(index: Number)
change切换分类项时触发(item: Object | String, index: Number)

基于 MIT 协议开源发行

H5 模拟器 (sidebar)☀️🔄