Skip to content

Anchor 锚点 ​

锚点导航组件,用于长页面的区块定位滚动与当前区块联动高亮,支持嵌套层级、水平方向与吸附。

引入 ​

html
<script
  type="module"
  src="./node_modules/easy-component-ui/dist/components/ea-anchor.js"
></script>
js
import "easy-component-ui/ea-anchor";

自定义样式 ​

移步到 CSS Part 和 CSS Custom Properties。

基础用法 ​

默认垂直排列。滚动页面时高亮随当前示例联动,点击链接平滑滚动到对应示例位置。

查看代码
html
<ea-anchor>
  <ea-anchor-link href="#basic" heading="基础用法"></ea-anchor-link>
  <ea-anchor-link href="#offset" heading="指定偏移"></ea-anchor-link>
  <ea-anchor-link href="#horizontal" heading="水平方向"></ea-anchor-link>
  <ea-anchor-link href="#nested" heading="嵌套目录"></ea-anchor-link>
  <ea-anchor-link href="#affix" heading="吸附导航"></ea-anchor-link>
  <ea-anchor-link href="#disabled" heading="禁用链接"></ea-anchor-link>
</ea-anchor>

指定偏移 ​

设置 offset="64",点击链接后目标示例停在距页面顶部 64px 处。

查看代码
html
<ea-anchor offset="64">
  <ea-anchor-link href="#basic" heading="基础用法"></ea-anchor-link>
  <ea-anchor-link href="#offset" heading="指定偏移"></ea-anchor-link>
  <ea-anchor-link href="#horizontal" heading="水平方向"></ea-anchor-link>
  <ea-anchor-link href="#nested" heading="嵌套目录"></ea-anchor-link>
  <ea-anchor-link href="#affix" heading="吸附导航"></ea-anchor-link>
  <ea-anchor-link href="#disabled" heading="禁用链接"></ea-anchor-link>
</ea-anchor>

水平方向 ​

设置 direction="horizontal",链接横向排列,marker 为底部下划线;滚动轴由容器实际可滚动方向自动判定,页面纵向滚动时同样联动高亮。

查看代码
html
<ea-anchor direction="horizontal">
  <ea-anchor-link href="#basic" heading="基础用法"></ea-anchor-link>
  <ea-anchor-link href="#offset" heading="指定偏移"></ea-anchor-link>
  <ea-anchor-link href="#horizontal" heading="水平方向"></ea-anchor-link>
  <ea-anchor-link href="#nested" heading="嵌套目录"></ea-anchor-link>
  <ea-anchor-link href="#affix" heading="吸附导航"></ea-anchor-link>
  <ea-anchor-link href="#disabled" heading="禁用链接"></ea-anchor-link>
</ea-anchor>

嵌套目录 ​

父级链接包含子级时,子级高亮会联动父级;此处按分组组织同一套示例链接。

查看代码
html
<ea-anchor>
  <ea-anchor-link href="#basic" heading="基础用法">
    <ea-anchor-link href="#offset" heading="指定偏移"></ea-anchor-link>
    <ea-anchor-link href="#horizontal" heading="水平方向"></ea-anchor-link>
  </ea-anchor-link>
  <ea-anchor-link href="#nested" heading="嵌套目录">
    <ea-anchor-link href="#affix" heading="吸附导航"></ea-anchor-link>
    <ea-anchor-link href="#disabled" heading="禁用链接"></ea-anchor-link>
  </ea-anchor-link>
</ea-anchor>

吸附导航 ​

设置 affix 与 offset="20",导航随页面滚动吸附在距顶部 20px 处。下方示例把导航放在右侧栏内,滚动整页即可观察其全程吸附效果。

区块一
区块二
区块三
查看代码
html
<div class="anchor-demo-layout">
  <div class="anchor-demo-pane">
    <div class="anchor-demo-block">区块一</div>
    <div class="anchor-demo-block">区块二</div>
    <div class="anchor-demo-block">区块三</div>
  </div>
  <div class="anchor-demo-aside">
    <ea-anchor affix offset="20">
      <ea-anchor-link href="#basic" heading="基础用法"></ea-anchor-link>
      <ea-anchor-link href="#offset" heading="指定偏移"></ea-anchor-link>
      <ea-anchor-link href="#horizontal" heading="水平方向"></ea-anchor-link>
      <ea-anchor-link href="#nested" heading="嵌套目录"></ea-anchor-link>
      <ea-anchor-link href="#affix" heading="吸附导航"></ea-anchor-link>
      <ea-anchor-link href="#disabled" heading="禁用链接"></ea-anchor-link>
    </ea-anchor>
  </div>
</div>

禁用链接 ​

此处将「吸附导航」一项设为 disabled,点击与高亮均不响应。

查看代码
html
<ea-anchor>
  <ea-anchor-link href="#basic" heading="基础用法"></ea-anchor-link>
  <ea-anchor-link href="#offset" heading="指定偏移"></ea-anchor-link>
  <ea-anchor-link href="#horizontal" heading="水平方向"></ea-anchor-link>
  <ea-anchor-link href="#nested" heading="嵌套目录"></ea-anchor-link>
  <ea-anchor-link href="#affix" heading="吸附导航" disabled></ea-anchor-link>
  <ea-anchor-link href="#disabled" heading="禁用链接"></ea-anchor-link>
</ea-anchor>

指定滚动容器 ​

设置 target="#container-scroll",锚点随容器内滚动联动,而不是页面滚动。

API · 容器内滚动
主题 · 容器内滚动
常见问题 · 容器内滚动
查看代码
html
<ea-anchor target="#container-scroll">
  <ea-anchor-link href="#container-api" heading="API"></ea-anchor-link>
  <ea-anchor-link href="#container-theme" heading="主题"></ea-anchor-link>
  <ea-anchor-link href="#container-faq" heading="常见问题"></ea-anchor-link>
</ea-anchor>

<div class="anchor-demo-scroll-box" id="container-scroll">
  <section id="container-api" class="anchor-demo-block anchor-demo-block-lg">
    API · 容器内滚动
  </section>
  <section id="container-theme" class="anchor-demo-block anchor-demo-block-lg">
    主题 · 容器内滚动
  </section>
  <section id="container-faq" class="anchor-demo-block anchor-demo-block-lg">
    常见问题 · 容器内滚动
  </section>
</div>

Anchor API ​

Anchor Attributes ​

参数说明类型可选值默认值
target滚动容器选择器,window 表示页面滚动String—window
offset滚动定位时目标区块距容器顶部的偏移量(用于固定头部)Number—0
offset-boundary滚动高亮判定「当前区块」的顶部边界容差Number—15
duration滚动动画时长(毫秒),0 表示瞬时跳转Number—300
direction排列方向(仅决定排列与 marker 方向,不影响滚动轴)Stringvertical | horizontalvertical
marker是否显示当前项滑块指示器Boolean—true
affix是否吸附:容器为垂直方向时吸附顶部,水平方向时吸附左侧Boolean—false
replace点击后使用 replaceState 同步 hash(默认 pushState)Boolean—false
active当前高亮链接的 href(可外部读写,实现受控)String—""

offset 与 offset-boundary 均以 offset 开头但语义不同:offset 服务于点击定位(滚动终点偏移),offset-boundary 服务于滚动高亮(判定当前区块的顶部边界容差),默认值也不同。

Anchor CSS Part ​

用法可参考 MDN ::part()伪类

名称说明
container导航容器元素
marker当前项滑块指示元素

Anchor Slots ​

名称说明
default默认插槽,用于放置 ea-anchor-link

Anchor Methods ​

方法名说明参数
scrollToAnchor程序化滚动到指定区块并高亮href: string

Anchor Events ​

事件名说明回调参数(event.detail)
ea-click点击锚点链接时触发{ href, heading, targetEl }
ea-change当前高亮链接变化时触发{ href, heading }

Anchor CSS Custom Properties ​

属性名说明默认值
--ea-anchor-font-size字号var(--font-size-md)
--ea-anchor-text-color文本颜色var(--grey-700)
--ea-anchor-text-color-active激活文本颜色var(--blue-500)
--ea-anchor-marker-color滑块颜色var(--blue-500)
--ea-anchor-marker-width滑块粗细2px
--ea-anchor-marker-offset滑块偏移量0px
--ea-anchor-marker-size滑块长度0px
--ea-anchor-offset吸附偏移量0px
--ea-anchor-transition过渡时长var(--transition-normal)
参数说明类型可选值默认值
href目标区块选择器String—""
heading链接标题String—""
disabled是否禁用该链接Boolean—false
active是否为当前高亮链接Boolean—false

用法可参考 MDN ::part()伪类

名称说明
container链接容器元素
link链接元素
heading标题元素
名称说明
heading自定义链接标题内容
default嵌套的子级 ea-anchor-link
属性名说明默认值
--ea-anchor-link-text-color文本颜色var(--grey-700)
--ea-anchor-link-text-color-active激活文本颜色var(--blue-500)
--ea-anchor-link-text-color-disabled禁用文本颜色var(--grey-400)
--ea-anchor-link-indent子级缩进var(--spacing-lg)
--ea-anchor-link-marker-gap与滑块的间隙var(--spacing-lg)
--ea-anchor-link-transition过渡时长var(--transition-fast)