Skip to content

Anchor 锚点 ​

在页面或滚动容器内显示章节目录,点击跳转并随滚动高亮当前章节。

何时使用 ​

  • 文档、设置页、长表单需要快速跳转到不同章节。
  • 内容在独立 ScrollBox 中滚动,并希望目录同步高亮。
  • 页面级菜单或应用路由使用 Menu 导航菜单;平级视图切换使用 Tabs 标签页。
  • 需要固定目录或工具条时,可与 Affix 固钉 一起使用。

在线体验 ​

Anchor 锚点 / Affix 固钉 / BackTop · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
)

var anchorTarget ui.ID

var anchorItems = []ui.AnchorItem{
    {Key: "intro", Title: "简介"},
    {Key: "usage", Title: "用法"},
}

func page(c *ui.Context) {
    ui.Row(c, func() {
        ui.Anchor(c, anchorItems).Target(anchorTarget).Show()
        scroll := ui.Scroll(c, func() {
            ui.AnchorSection(c, "intro", func() { ui.Title(c, "简介", 4) })
            ui.AnchorSection(c, "usage", func() { ui.Title(c, "用法", 4) })
        }, layout.H(320), layout.Grow(1))
        anchorTarget = scroll.ID()
    }, layout.Gap(24))
}

Anchor 链接的 Key 必须与 AnchorSection 的 key 一致;Target 指向承载这些章节的滚动容器。

常见用法 ​

多级目录 ​

AnchorItem.Children 可表达二级目录,子项同样对应页面中的 AnchorSection。

go
import "github.com/tangoui/tango/ui"

var items = []ui.AnchorItem{
    {Key: "basic", Title: "基础"},
    {Key: "advanced", Title: "进阶", Children: []ui.AnchorItem{{Key: "offset", Title: "偏移量"}}},
}

func page(c *ui.Context) {
    ui.Anchor(c, items).Show()
}

横向锚点 ​

Horizontal() 把链接排成一行,适合页面顶部的章节切换;NoInk() 隐藏指示条。

go
import "github.com/tangoui/tango/ui"

var items = []ui.AnchorItem{{Key: "overview", Title: "概览"}, {Key: "api", Title: "API"}}

func page(c *ui.Context) {
    ui.Anchor(c, items).Horizontal().NoInk().Show()
}

固定标题偏移 ​

如果滚动容器顶部有固定标题,用 TargetOffset 让跳转与高亮判断留出标题高度。

go
import "github.com/tangoui/tango/ui"

var target ui.ID
var items = []ui.AnchorItem{{Key: "section", Title: "章节"}}

func page(c *ui.Context) {
    ui.Anchor(c, items).Target(target).TargetOffset(56).Show()
}

监听当前章节 ​

Active() 返回当前高亮 key,Clicked() 返回本帧点击的 key,可用于同步地址栏或统计。

go
import "github.com/tangoui/tango/ui"

var items = []ui.AnchorItem{{Key: "a", Title: "A"}, {Key: "b", Title: "B"}}
var lastClick string

func page(c *ui.Context) {
    anchor := ui.Anchor(c, items).Show()
    if key := anchor.Clicked(); key != "" {
        lastClick = key
    }
    ui.Text(c, "当前:"+anchor.Active()+",最近点击:"+lastClick)
}

主题与样式 ​

Anchor 的链接颜色、激活颜色、指示条和间距由 theme.Components.Anchor 控制。Ant Design 默认使用竖向指示条,Element Plus 可通过 NoInk() 获得无下划线样式,shadcn 使用更低对比度的文本和细线。

注意事项 ​

  • AnchorSection key 必须唯一且与目录项一致;动态内容变化时也要保持 key 稳定。
  • Target 使用滚动容器 ID;ID 在当前帧创建后才知道,通常保存到页面状态,下一帧再传给 Anchor。
  • 章节非常多时,目录应保持层级简短,避免把每一个小段落都放入 Anchor。
  • 链接可通过键盘聚焦和激活;章节标题应使用语义清晰的文本。
  • RTL 下竖向指示条与横向排列会自动镜像。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Anchor 锚点 / Affix 固钉 / BackTop

gallery 演示 Anchor 的具名 Render 函数,源码 page_nav_anchor.go#L17。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Anchor ​

go
func Anchor(c *Context, items []AnchorItem) *AnchorW

Anchor starts a table of contents; call Target with the scroll container holding the AnchorSections, then Show.

返回 *AnchorW,可链式调用的方法见 AnchorW 方法。

ui.AnchorSection ​

go
func AnchorSection(c *Context, key string, fn func(), opts ...layout.Option) *AnchorSectionW

AnchorSection wraps a block of page content that an Anchor link with the same key scrolls to and highlights.

返回 *AnchorSectionW,可链式调用的方法见 AnchorSectionW 方法。

AnchorW 方法 ​

ui.AnchorW:AnchorW is an in-page table of contents that follows scrolling.

方法说明签名
ActiveActive returns the highlighted key.Active() string
BoundsBounds sets how close (px) a section's top must come to the offset line to become active (antd bounds 5, Element bound 15; default from AnchorToken.Bounds).Bounds(px float32) *AnchorW
ChangedChanged reports that the active link changed this frame.Changed() bool
ClickedClicked returns the key clicked this frame.Clicked() string
CurrentAnchorCurrentAnchor customizes the highlighted link (antd getCurrentAnchor): fn gets the key scrolling selected and returns the key to highlight.CurrentAnchor(fn func(active string) string) *AnchorW
DirectionDirection lays the links out "vertical" (default) or "horizontal" (antd direction, Element direction).Direction(d string) *AnchorW
HorizontalHorizontal lays links out in a row with a bottom ink bar.Horizontal() *AnchorW
NoInkNoInk hides the ink rail (Element "underline" off).NoInk() *AnchorW
ShowShow builds the anchor.Show() *AnchorW
TargetTarget sets the scroll container (ScrollBox.ID()).Target(id ID) *AnchorW
TargetOffsetTargetOffset scrolls sections this far below the container top (fixed headers) and uses it for the active test.TargetOffset(px float32) *AnchorW
TypeType picks Element's look: "underline" draws the rail with a thin marker, "default" only a rounded marker.Type(t string) *AnchorW

AnchorSectionW 类型 ​

ui.AnchorSectionW:AnchorSectionW is the section AnchorSection returns; it embeds the section's Box.

字段说明类型
Box*Box(嵌入)

AnchorSectionW 嵌入通用容器 *ui.Box,可用的布局与样式方法见 Box 的 API;下表只列 AnchorSectionW 自己的方法。

AnchorItem 参数类型 ​

ui.AnchorItem:AnchorItem is one link of an Anchor; Key matches an AnchorSection.

字段说明类型
Keystring
Titlestring
Children[]AnchorItem

相关链接 ​

以 MIT 许可发布