Vant4 IndexBar 索引欄

2023-02-16 17:56 更新

介紹

用于列表的索引分類(lèi)顯示和快速定位。

引入

通過(guò)以下方式來(lái)全局注冊(cè)組件,更多注冊(cè)方式請(qǐng)參考組件注冊(cè)。

import { createApp } from 'vue';
import { IndexBar, IndexAnchor } from 'vant';

const app = createApp();
app.use(IndexBar);
app.use(IndexAnchor);

代碼演示

基礎(chǔ)用法

點(diǎn)擊索引欄時(shí),會(huì)自動(dòng)跳轉(zhuǎn)到對(duì)應(yīng)的 ?IndexAnchor? 錨點(diǎn)位置。

<van-index-bar>
  <van-index-anchor index="A" />
  <van-cell title="文本" />
  <van-cell title="文本" />
  <van-cell title="文本" />

  <van-index-anchor index="B" />
  <van-cell title="文本" />
  <van-cell title="文本" />
  <van-cell title="文本" />

  ...
</van-index-bar>

自定義索引列表

可以通過(guò) ?index-list? 屬性自定義展示的索引字符列表。

<van-index-bar :index-list="indexList">
  <van-index-anchor index="1">標(biāo)題1</van-index-anchor>
  <van-cell title="文本" />
  <van-cell title="文本" />
  <van-cell title="文本" />

  <van-index-anchor index="2">標(biāo)題2</van-index-anchor>
  <van-cell title="文本" />
  <van-cell title="文本" />
  <van-cell title="文本" />

  ...
</van-index-bar>
export default {
  setup() {
    return {
      indexList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    };
  },
};

API

IndexBar Props

參數(shù) 說(shuō)明 類(lèi)型 默認(rèn)值
index-list 索引字符列表 string[] | number[] A-Z
z-index z-index 層級(jí) number | string 1
sticky 是否開(kāi)啟錨點(diǎn)自動(dòng)吸頂 boolean true
sticky-offset-top 錨點(diǎn)自動(dòng)吸頂時(shí)與頂部的距離 number 0
highlight-color 索引字符高亮顏色 string #1989fa
teleport v3.0.19 指定索引欄掛載的節(jié)點(diǎn) string | Element -

IndexAnchor Props

參數(shù) 說(shuō)明 類(lèi)型 默認(rèn)值
index 索引字符 number | string -

IndexBar Events

事件名 說(shuō)明 回調(diào)參數(shù)
select 點(diǎn)擊索引欄的字符時(shí)觸發(fā) index: number | string
change 當(dāng)前高亮的索引字符變化時(shí)觸發(fā) index: number | string

IndexBar 方法

通過(guò) ref 可以獲取到 IndexBar 實(shí)例并調(diào)用實(shí)例方法,詳見(jiàn)組件實(shí)例方法。

方法名 說(shuō)明 參數(shù) 返回值
scrollTo 滾動(dòng)到指定錨點(diǎn) index: number | string -

類(lèi)型定義

組件導(dǎo)出以下類(lèi)型定義:

import type { IndexBarProps, IndexAnchorProps, IndexBarInstance } from 'vant';

?IndexBarInstance? 是組件實(shí)例的類(lèi)型,用法如下:

import { ref } from 'vue';
import type { IndexBarInstance } from 'vant';

const indexBarRef = ref<IndexBarInstance>();

indexBarRef.value?.scrollTo('B');

IndexAnchor Slots

名稱(chēng) 說(shuō)明
default 錨點(diǎn)位置顯示內(nèi)容,默認(rèn)為索引字符

主題定制

樣式變量

組件提供了下列 CSS 變量,可用于自定義樣式,使用方法請(qǐng)參考 ConfigProvider 組件

名稱(chēng) 默認(rèn)值 描述
--van-index-bar-sidebar-z-index 2 -
--van-index-bar-index-font-size var(--van-font-size-xs) -
--van-index-bar-index-line-height var(--van-line-height-xs) -
--van-index-bar-index-active-color var(--van-primary-color) -
--van-index-anchor-z-index 1 -
--van-index-anchor-padding 0 var(--van-padding-md) -
--van-index-anchor-text-color var(--van-text-color) -
--van-index-anchor-font-weight var(--van-font-bold) -
--van-index-anchor-font-size var(--van-font-size-md) -
--van-index-anchor-line-height 32px -
--van-index-anchor-background transparent -
--van-index-anchor-sticky-text-color var(--van-primary-color) -
--van-index-anchor-sticky-background var(--van-background-2) -


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)