Skip to content
在新窗口打开实时预览

IndexList 页面索引列表 ​

跟随页面滚动,通过右侧索引快速定位分组内容,与 IndexAnchor 配合使用。

如果列表位于弹窗、抽屉或定高区域,请使用 IndexListPanel。

平台兼容性 ​

H5App微信小程序支付宝小程序
√√√√

基础用法 ​

页面需要在 onPageScroll 中记录滚动位置并传给组件。

vue
<script setup lang="ts">
import { onPageScroll } from '@dcloudio/uni-app';
import { ref } from 'vue';

const scrollTop = ref(0);
onPageScroll(event => {
    scrollTop.value = event.scrollTop;
});
</script>

<template>
    <snake-index-list :index-list="['A', 'B']" :scroll-top="scrollTop" @select="onSelect">
        <snake-index-anchor index="A" />
        <view>Apple</view>
        <snake-index-anchor index="B" />
        <view>Banana</view>
    </snake-index-list>
</template>

偏移与异步数据 ​

页面有固定导航栏时传入 offset-top,让索引跳转和吸顶锚点避开导航栏。异步加载、折叠展开或图片加载改变了分组高度后,等待视图更新并调用 update() 重新测量。

ts
groups.value = await getContacts();
await nextTick();
indexListRef.value?.update();

点击索引会触发 select,页面滚动导致当前分组改变会触发 change;业务只需处理选中态或辅助提示,滚动定位由组件完成。

API ​

Props ​

参数说明类型默认值
indexList右侧索引值Array<string | number>[]
scrollTop页面当前滚动位置number | string0
zIndex索引栏和吸顶锚点层级number965
sticky锚点是否吸顶booleantrue
offsetTop页面吸顶与滚动目标偏移string | number0
activeColor / inactiveColor当前和默认索引颜色string主题色 / 弱文本色
railStyle右侧索引栏样式string | object | array''
customClass / customStyle自定义类名和样式string | object | array''

裸数字和 rpx 字符串按 rpx 处理,显式 px 字符串按像素处理。

Events ​

事件名触发时机参数
select点击或拖动选择索引索引值
change页面滚动导致当前分组变化{ index, value }

Expose ​

方法说明
scrollTo(indexOrValue)页面滚动到序号或索引值
update()页面布局变化后重新测量锚点

Slots ​

插槽名说明
default索引锚点及对应的分组内容

主题变量 ​

变量名说明
--snake-color-primary当前索引默认颜色
--snake-color-text-muted未激活索引默认颜色
--snake-color-primary-light当前索引提示层背景颜色

从旧版迁移 ​

旧版 snake-index-list 内置 scroll-view。升级后,页面场景需要传入页面 scrollTop;原弹窗和定高容器场景改用 snake-index-list-panel。

为 uni-app 商城与 CMS 场景设计