Skip to content

IndexBar 索引栏 - Vant 4

📑 IndexBar 索引栏 ​

🎯 介绍 ​

IndexBar 组件就像是一个贴心的"导航小助手" 🧭!它为长列表提供了快速定位功能,让用户能够像翻字典一样迅速找到想要的内容。无论是通讯录、城市列表还是商品分类,都能让用户享受丝滑的浏览体验,告别无尽的滚动之苦!

📦 引入 ​

通过以下方式来全局注册组件,更多注册方式请参考组件注册。

js
import { createApp } from'vue'; import { IndexBar, IndexAnchor } from'vant'; const app = createApp(); app.use(IndexBar); app.use(IndexAnchor);

🚀 代码演示 ​

🔧 基础用法 ​

点击索引栏时,会自动跳转到对应的 IndexAnchor 锚点位置,就像瞬间传送一样神奇!✨

html

🎨 自定义索引列表 ​

可以通过 index-list 属性自定义展示的索引字符列表,让索引栏变身为你专属的导航工具!无论是数字、字母还是特殊符号,都能轻松驾驭。

html
js
exportdefault { setup() { return { indexList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], }; }, };

API ​

IndexBar Props ​

参数说明类型默认值
index-list索引字符列表*(stringnumber)[]*
z-indexz-index 层级*numberstring*
sticky是否开启锚点自动吸顶booleantrue
sticky-offset-top锚点自动吸顶时与顶部的距离number0
highlight-color索引字符高亮颜色string#1989fa
teleport指定索引栏挂载的节点*stringElement*

IndexAnchor Props ​

参数说明类型默认值
index索引字符*numberstring*

IndexBar Events ​

事件名说明回调参数
select点击索引栏的字符时触发*index: number
change当前高亮的索引字符变化时触发*index: number

IndexBar 方法 ​

通过 ref 可以获取到 IndexBar 实例并调用实例方法,详见组件实例方法。

方法名说明参数返回值
scrollTo滚动到指定锚点*index: numberstring*

类型定义 ​

组件导出以下类型定义:

ts
importtype { IndexBarProps, IndexAnchorProps, IndexBarInstance } from'vant';

IndexBarInstance 是组件实例的类型,用法如下:

ts
import { ref } from'vue'; importtype { IndexBarInstance } from'vant'; const indexBarRef = ref<IndexBarInstance>(); indexBarRef.value?.scrollTo('B');

IndexAnchor Slots ​

名称说明
default锚点位置显示内容,默认为索引字符

主题定制 ​

样式变量 ​

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件。

名称默认值描述
--van-index-bar-sidebar-z-index2-
--van-index-bar-index-font-sizevar(--van-font-size-xs)-
--van-index-bar-index-line-heightvar(--van-line-height-xs)-
--van-index-bar-index-active-colorvar(--van-primary-color)-
--van-index-anchor-z-index1-
--van-index-anchor-padding0 var(--van-padding-md)-
--van-index-anchor-text-colorvar(--van-text-color)-
--van-index-anchor-font-weightvar(--van-font-bold)-
--van-index-anchor-font-sizevar(--van-font-size-md)-
--van-index-anchor-line-height32px-
--van-index-anchor-backgroundtransparent-
--van-index-anchor-sticky-text-colorvar(--van-primary-color)-
--van-index-anchor-sticky-backgroundvar(--van-background-2)-

📚 相关文档 ​

📋 列表组件 ​

🎯 导航组件 ​

🔍 搜索与定位 ​

🎨 设计组件 ​

🛠️ 开发指南 ​

基於Vant構建的企業級移動端解決方案