Anchor 锚点
用于跳转到页面指定位置。
基础用法
最简单的用法。
vue
<template>
<ExAnchor @change="handleChange">
<ExAnchorLink href="#basic" title="基础用法" />
<ExAnchorLink href="#fixed" title="固定位置" />
<ExAnchorLink href="#api" title="API" />
</ExAnchor>
</template>
<script setup>
const handleChange = (href) => {
console.log('锚点变化:', href)
}
</script>嵌套锚点
锚点支持嵌套。
vue
<template>
<ExAnchor>
<ExAnchorLink href="#components" title="组件">
<ExAnchorLink href="#basic-components" title="基础组件" />
<ExAnchorLink href="#form-components" title="表单组件" />
</ExAnchorLink>
<ExAnchorLink href="#guide" title="指南">
<ExAnchorLink href="#installation" title="安装" />
<ExAnchorLink href="#usage" title="使用" />
</ExAnchorLink>
</ExAnchor>
</template>自定义偏移量
设置锚点滚动偏移量。
vue
<template>
<ExAnchor :offset-top="100" :target-offset="50">
<ExAnchorLink href="#section1" title="第一节" />
<ExAnchorLink href="#section2" title="第二节" />
<ExAnchorLink href="#section3" title="第三节" />
</ExAnchor>
</template>API
Anchor Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| offset-top | 距离窗口顶部达到指定偏移量后触发 | number | 0 |
| bounds | 锚点区域边界 | number | 5 |
| target-offset | 设置锚点滚动偏移量 | number | - |
| container | 指定滚动的容器 | () => HTMLElement | Window | () => window |
| affix | 固定模式 | boolean | true |
Anchor Events
| 事件名 | 说明 | 类型 |
|---|---|---|
| click | 点击锚点时触发 | (href: string, item: AnchorItem) => void |
| change | 锚点改变时触发 | (href: string) => void |
Anchor Methods
| 方法名 | 说明 | 类型 |
|---|---|---|
| scrollToAnchor | 滚动到指定锚点 | (href: string) => void |
AnchorLink Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| href | 锚点链接 | string | - |
| title | 锚点标题 | string | - |
AnchorLink Events
| 事件名 | 说明 | 类型 |
|---|---|---|
| click | 点击锚点时触发 | (href: string) => void |