Anchor
通过滚动跟踪、缩进和历史记录来导航文档部分。
何时使用
锚点将目录链接到长页面的各个部分,并在滚动时显示活动部分。它适用于具有强标题标识符的文档、订购条件和文章。要在页面之间导航,请使用 Menu。
集装箱、码头和垫料
无需设置即可使用窗口滚动。对于本地范围,getContainer 返回其 HTMLElement;与 Affix.target 不同,此 getter 的公共类型不允许为 null。使用 bind:this 绑定节点,如示例中所示。可以通过 ConfigProvider.getTargetContainer 设置通用 getter。
offsetTop 定义对接位置。 targetOffset 设置过渡和定义当前节时的目标偏移量;特定项目的值优先。bounds—确定活动分区的容差。如果标题是固定的,则根据其实际高度设置缩进:否则标题将出现在其下方。
Affix 接受 boolean 或不带 offsetTop、target 和 children 的 Affix 设置。 false 将列表留在流中; showInkInFixed 将指示器保持在此模式。 固定在容器中 还显示了 CSS 包装器:scroll target 本身不会将固定节点剪切到卡片的边框。
的历史、设计和可用性
对于本地片段链接,组件通过 pushState 或 replaceState 更改地址。replace 可以设置为整个锚点或单个项目。target 指的是链接,允许您以不同的方式打开常规地址。
在 onClick 上调用 event.preventDefault() 会取消历史记录更改,但不会取消 Anchor 的内部滚动。因此,可以滚动文档卡而不改变整个页面的地址。此历史记录不会触发本机片段转换:使用应用程序状态,而不仅仅是 CSS :target 来设置当前部分的样式。
链接保留本机 Tab 和 Enter。组件不会自动添加 aria-current;如有必要,请为您自己的作品中的当前部分设置可用标签。 Root、item、itemTitle 和指示器通过 classNames/styles 配置; styles 值是 CSS 字符串。一般默认值由 ConfigProvider.anchor 提供,主题缩进由 Anchor 组件设计令牌(design tokens)提供。
prefixCls 指定完整的本地 Anchor 前缀。空字符串和 undefined 使用带有 -anchor 后缀的 ConfigProvider.prefixCls。对于item,规则是不同的:缺少的 prefixCls 继承Anchor所有者允许的前缀,空字符串返回提供者的前缀,非空字符串设置自己的完整链接前缀。显式项目覆盖需要单独的 CSS,如 前缀示例 中。本地 Anchor.prefixCls 不重命名Affix; theme.cssVar.prefix 单独设置主题变量的命名空间。
通过链接继承本地前缀是与 Ant Design 6.6.8 的特意区别:没有显式前缀的 items 保留提供者前缀,这可能会破坏指示器的设计和位置。这里的指标跟随活动链接并带有单独的 item.prefixCls。
示例
垂直导航
滚动所选容器时,嵌套 items 和部分突出显示。禁用固定以隔离该卡。
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-basic-intro', title: "描述" },
{
key: 'delivery', href: '#anchor-basic-delivery', title: "送货",
children: [{ key: 'payment', href: '#anchor-basic-payment', title: "支付" }]
}
])}
showInkInFixed
/>
<div
class="area"
bind:this={area}
role="region"
aria-label={("基本锚固部分")}
tabindex="0"
>
<div id="anchor-basic-intro" class="section">{("项目描述")}</div>
<div id="anchor-basic-delivery" class="section">{("项目交付")}</div>
<div id="anchor-basic-payment" class="section">{("项目付款")}</div>
</div>
</div>
横向导航
水平锚点使用平面列表。长菜单在自己的区域内滚动。
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
.layout { grid-template-columns: minmax(0, 1fr); }
</style>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-horizontal-intro', title: "描述" },
{ key: 'delivery', href: '#anchor-horizontal-delivery', title: "送货" },
{ key: 'payment', href: '#anchor-horizontal-payment', title: "支付" }
])}
direction="horizontal"
showInkInFixed
/>
<div class="area" bind:this={area} role="region" aria-label={("水平锚固部分")} tabindex="0">
<div id="anchor-horizontal-intro" class="section">{("项目描述")}</div>
<div id="anchor-horizontal-delivery" class="section">{("项目交付")}</div>
<div id="anchor-horizontal-payment" class="section">{("项目付款")}</div>
</div>
</div>
不带紧固
affix=false 将导航保留在流程中。 showInkInFixed 控制指示器;维护分区跟踪。
<script lang="ts">
import { Anchor, Checkbox } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let ink = $state(false);
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div
class="controls"
><Checkbox
bind:checked={ink}
>{("静态锚点指示器")}</Checkbox></div>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-static-intro', title: "描述" },
{ key: 'delivery', href: '#anchor-static-delivery', title: "送货" },
{ key: 'payment', href: '#anchor-static-payment', title: "支付" }
])}
showInkInFixed={ink}
/>
<div class="area" bind:this={area} role="region" aria-label={("静态锚固部分")} tabindex="0">
<div id="anchor-static-intro" class="section">{("项目描述")}</div>
<div id="anchor-static-delivery" class="section">{("项目交付")}</div>
<div id="anchor-static-payment" class="section">{("项目付款")}</div>
</div>
</div>
点击处理
preventDefault 防止 URL 被写入历史记录。 Anchor 继续滚动容器并报告所选链接。
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let clicked = $state('');
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
output {
display: block;
margin-top: 16px;
overflow-wrap: anywhere;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={(event, link) => {
event.preventDefault();
clicked = link.href;
}}
items={([
{ key: 'intro', href: '#anchor-click-intro', title: "描述" },
{ key: 'delivery', href: '#anchor-click-delivery', title: "送货" },
{ key: 'payment', href: '#anchor-click-payment', title: "支付" }
])}
/>
<div
class="area"
bind:this={area}
role="region"
aria-label={("部分单击锚点")}
tabindex="0"
>
<div id="anchor-click-intro" class="section">{("项目描述")}</div>
<div id="anchor-click-delivery" class="section">{("项目交付")}</div>
<div id="anchor-click-payment" class="section">{("项目付款")}</div>
</div>
</div>
<output aria-label={("单击锚点")}>{(clicked
|| ("链接尚未被点击"))}</output>
自有照明
getCurrentAnchor 设置活动链接,无论实际滚动如何。选择背光保持示例的状态。
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let highlighted = $state('#anchor-highlight-intro');
let resolveHighlight = $derived.by(() => {
const href = highlighted;
return () => href;
});
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="controls">
<label>{("突出显示部分 ")}<select bind:value={highlighted}>
<option value="#anchor-highlight-intro">{("描述")}</option>
<option value="#anchor-highlight-delivery">{("送货")}</option>
<option value="#anchor-highlight-payment">{("支付")}</option>
</select></label>
</div>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-highlight-intro', title: "描述" },
{ key: 'delivery', href: '#anchor-highlight-delivery', title: "送货" },
{ key: 'payment', href: '#anchor-highlight-payment', title: "支付" }
])}
showInkInFixed
getCurrentAnchor={resolveHighlight}
/>
<div class="area" bind:this={area} role="region" aria-label={("突出显示锚点部分")} tabindex="0">
<div id="anchor-highlight-intro" class="section">{("项目描述")}</div>
<div id="anchor-highlight-delivery" class="section">{("项目交付")}</div>
<div id="anchor-highlight-payment" class="section">{("项目付款")}</div>
</div>
</div>
目标偏移量
targetOffset 在该部分上方留出空间。对于一个链接,其自己的 targetOffset 优先于通用链接。
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-offset-intro', title: "缩进 32 像素" },
{ key: 'delivery', href: '#anchor-offset-delivery', title: "边距 64 像素", targetOffset: 64 },
{ key: 'payment', href: '#anchor-offset-payment', title: "再次 32 像素" }
])}
showInkInFixed
targetOffset={32}
/>
<div class="area" bind:this={area} role="region" aria-label={("偏移锚固部分")} tabindex="0">
<div style="
height: 64px;
padding: 8px;
box-sizing: border-box;
">
{("部分上方区域")}
</div>
<div id="anchor-offset-intro" class="section">{("项目描述")}</div>
<div id="anchor-offset-delivery" class="section">{("项目交付")}</div>
<div id="anchor-offset-payment" class="section">{("项目付款")}</div>
</div>
</div>
更改活动分区
onChange 反映单击和手动滚动时各部分之间的转换。故事仅限于最后五件事。
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let events = $state<string[]>([]);
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
output {
display: block;
margin-top: 16px;
overflow-wrap: anywhere;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-change-intro', title: "描述" },
{ key: 'delivery', href: '#anchor-change-delivery', title: "送货" },
{ key: 'payment', href: '#anchor-change-payment', title: "支付" }
])}
showInkInFixed
onChange={value => { events = [...events, value || "开始"].slice(-5); }}
/>
<div class="area" bind:this={area} role="region" aria-label={("更改锚点部分")} tabindex="0">
<div id="anchor-change-intro" class="section">{("项目描述")}</div>
<div id="anchor-change-delivery" class="section">{("项目交付")}</div>
<div id="anchor-change-payment" class="section">{("项目付款")}</div>
</div>
</div>
<output aria-label={("锚固截面的历史")}>{(events.join(' → ')
|| ("没有活动"))}</output>
转型历史
replace 会更改当前的历史记录条目,而不是添加新的历史记录条目。此示例有意更新地址栏中的片段。
<script lang="ts">
import { Anchor, Checkbox } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let replace = $state(true);
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
.layout { grid-template-columns: minmax(0, 1fr); }
</style>
<div class="controls">
<Checkbox bind:checked={replace}>
{("替换历史记录锚点")}
</Checkbox>
</div>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
items={([
{ key: 'intro', href: '#anchor-history-intro', title: "描述" },
{ key: 'delivery', href: '#anchor-history-delivery', title: "送货" },
{ key: 'payment', href: '#anchor-history-payment', title: "支付" }
])}
replace={replace}
showInkInFixed
/>
<div class="area" bind:this={area} role="region" aria-label={("历史锚点部分")} tabindex="0">
<div id="anchor-history-intro" class="section">{("项目描述")}</div>
<div id="anchor-history-delivery" class="section">{("项目交付")}</div>
<div id="anchor-history-payment" class="section">{("项目付款")}</div>
</div>
</div>
Semantic styles
styles 函数对 root、item、itemTitle 和指示器进行样式设置。当改变方向时,该函数接收当前的 props。
<script lang="ts">
import { Anchor, Checkbox } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let horizontal = $state(false);
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
.layout { grid-template-columns: minmax(0, 1fr); }
</style>
<div class="controls">
<Checkbox bind:checked={horizontal}>
{("水平语义锚")}
</Checkbox>
</div>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-semantic-intro', title: "描述" },
{ key: 'delivery', href: '#anchor-semantic-delivery', title: "送货" },
{ key: 'payment', href: '#anchor-semantic-payment', title: "支付" }
])}
showInkInFixed
direction={horizontal ? 'horizontal' : 'vertical'}
classNames={{ root: 'semantic-anchor-root', itemTitle: 'semantic-anchor-title' }}
styles={({ props }) => ({
root: `
padding: 12px;
border: 1px solid var(--ant-color-primary-border);
border-radius: 8px;
`,
item: props.direction === 'horizontal'
? 'padding-inline:8px'
: 'padding-block:2px',
itemTitle: 'font-weight:600',
indicator: 'background:var(--ant-color-success)'
})}
/>
<div class="area" bind:this={area} role="region" aria-label={("语义锚点部分")} tabindex="0">
<div id="anchor-semantic-intro" class="section">{("项目描述")}</div>
<div id="anchor-semantic-delivery" class="section">{("项目交付")}</div>
<div id="anchor-semantic-payment" class="section">{("项目付款")}</div>
</div>
</div>
组件代币
本地主题通过 theme.components.Anchor 更改链接的垂直和水平填充。
<script lang="ts">
import { Anchor, ConfigProvider } from '@woodger/antd';
let area = $state<HTMLDivElement>();
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<ConfigProvider theme={{
cssVar: {}, components: {
Anchor: {
linkPaddingBlock: 10,
linkPaddingInlineStart: 24
}
}
}}>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-tokens-intro', title: "描述" },
{ key: 'delivery', href: '#anchor-tokens-delivery', title: "送货" },
{ key: 'payment', href: '#anchor-tokens-payment', title: "支付" }
])}
showInkInFixed
/>
<div class="area" bind:this={area} role="region" aria-label={("令牌锚部分")} tabindex="0">
<div id="anchor-tokens-intro" class="section">{("项目描述")}</div>
<div id="anchor-tokens-delivery" class="section">{("项目交付")}</div>
<div id="anchor-tokens-payment" class="section">{("项目付款")}</div>
</div>
</div>
</ConfigProvider>
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
</script>
<style>
.frame {
transform: translate(0);
isolation: isolate;
overflow: hidden;
border-radius: 8px;
}
.area {
height: 220px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
}
.frame :global(.fixed-local-anchor) { top: 8px !important; }
.section {
min-height: 220px;
padding: 16px;
box-sizing: border-box;
}
.section:last-child { min-height: 300px; }
</style>
<div class="frame">
<div
class="area"
bind:this={area}
role="region"
aria-label={("锚定锚")}
tabindex="0"
>
<div style="height:48px"></div>
<Anchor affix={{ rootClassName: 'fixed-local-anchor' }} offsetTop={8}
getContainer={() => area!} direction="horizontal" targetOffset={48}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-fixed-intro', title: "开始" },
{ key: 'details', href: '#anchor-fixed-details', title: "细节" }
])}
style="background:var(--ant-color-bg-container)"
/>
<div id="anchor-fixed-intro" class="section">{("文档的开头")}</div>
<div id="anchor-fixed-details" class="section">{("文件详情")}</div>
</div>
</div>
锚点设置
一般语义默认值,替换记录并在更改缩进后重新计算指示器。
<script lang="ts">
import { Anchor, Checkbox, ConfigProvider } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let spacious = $state(false);
const items = [
{ key: 'intro', href: '#anchor-provider-intro', title: "描述" },
{ key: 'delivery', href: '#anchor-provider-delivery', title: "送货" },
{ key: 'payment', href: '#anchor-provider-payment', title: "支付" }
];
</script>
<style>
.settings { margin-bottom: 24px; }
.examples {
display: grid;
gap: 20px;
}
.area {
height: 180px;
overflow: auto;
border-radius: 8px;
background: var(--ant-color-fill-quaternary);
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
}
.section:last-child { min-height: 240px; }
</style>
<div class="settings">
<Checkbox bind:checked={spacious}>
{("增加链接缩进锚点")}
</Checkbox>
</div>
<ConfigProvider anchor={{
class: 'provider-anchor', style: 'border-radius:8px',
classNames: { root: 'context-anchor-root' },
styles: () => ({
root: 'border-radius:4px',
item: `padding-block:${spacious ? 12 : 2}px`,
itemTitle: 'font-weight:600',
indicator: 'background:#237804'
})
}}>
<div class="examples">
<Anchor items={(items)} affix={false} showInkInFixed getContainer={() => area!}
onClick={event => event.preventDefault()} class="local-anchor" style="border-radius:16px"
classNames={{ root: 'local-anchor-root' }} styles={{ root: 'border-radius:12px' }} />
<ConfigProvider anchor={{}}>
<p>{("空记录恢复标准外观:")}</p>
<Anchor items={(items)} affix={false} getContainer={() => area!}
onClick={event => event.preventDefault()} class="reset-anchor" />
</ConfigProvider>
<div bind:this={area} class="area" role="region" aria-label={("锚点配置部分")} tabindex="0">
<div id="anchor-provider-intro" class="section">{("项目描述")}</div>
<div id="anchor-provider-delivery" class="section">{("项目交付")}</div>
<div id="anchor-provider-payment" class="section">{("项目付款")}</div>
</div>
</div>
</ConfigProvider>
锚点和链接前缀
全局和局部 prefixCls、item 继承和显式空前缀。指示器继续跟随活动链接。
<script lang="ts">
import { Anchor, Checkbox, ConfigProvider, type AnchorItem } from '@woodger/antd';
let custom = $state(true);
let local = $state(true);
let area = $state<HTMLDivElement>();
let active = $state('');
const prefix = $derived(custom ? 'suite' : 'ant');
const items: AnchorItem[] = $derived([
{ key: 'owner', href: '#anchor-prefix-owner', title: "遗产" },
{
key: 'provider', href: '#anchor-prefix-provider', title: "提供者", prefixCls: '',
class: `prefix-anchor-override ${active === '#anchor-prefix-provider' ? 'is-active' : ''}`
},
{
key: 'custom', href: '#anchor-prefix-custom', title: "你自己的前缀", prefixCls: 'item-anchor',
class: `prefix-anchor-override ${active === '#anchor-prefix-custom' ? 'is-active' : ''}`
}
]);
</script>
<style>
.example {
display: grid;
gap: 20px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
.layout {
display: grid;
grid-template-columns: minmax(120px, 150px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
.layout :global(.prefix-anchor-override) { padding: 4px 0 4px 16px; }
.layout :global(.prefix-anchor-override > a) {
display: block;
color: var(--ant-color-text);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.layout :global(.prefix-anchor-override.is-active > a) { color: var(--ant-color-primary); }
p { margin: 0; }
output { overflow-wrap: anywhere; }
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="example">
<div class="controls">
<Checkbox bind:checked={custom}>{("公共前缀锚定套件")}</Checkbox>
<Checkbox bind:checked={local}>{("本地锚前缀")}</Checkbox>
</div>
<ConfigProvider prefixCls={prefix} theme={{ cssVar: { prefix: 'anchor-theme' } }}>
<div class="layout">
<Anchor
items={(items)}
prefixCls={local ? 'project-anchor' : ''}
class="prefix-anchor-main"
affix={false}
showInkInFixed
replace
getContainer={() => area!}
onChange={href => { active = href; }}
/>
<div class="area" bind:this={area} role="region"
aria-label={("带前缀的锚定部分")} tabindex="0">
<div id="anchor-prefix-owner" class="section">
{("该链接继承锚点所有者的前缀。")}
</div>
<div id="anchor-prefix-provider" class="section">
{("空的 item.prefixCls 选择公共提供者前缀。")}
</div>
<div id="anchor-prefix-custom" class="section">
{("非空 item.prefixCls 指定完整的自定义前缀。")}
</div>
</div>
</div>
</ConfigProvider>
<output
aria-label={("锚前缀")}
>{("\n 一般的: ")}{prefix}{(";班级: ")}{(local ? 'project-anchor' : prefix + '-anchor')}{(";章: ")}{(active || '—')}
</output>
<p>
{("对于具有显式 item.prefixCls 的两个链接,装饰是通过类单独指定的。更改前缀会保留链接和指示器的位置。")}
</p>
</div>
订购部分
已保存评论示例,其中包含描述、交付和付款的本地滚动。
<script lang="ts">
import { Anchor } from '@woodger/antd';
import { onMount } from 'svelte';
let navigation = $state<HTMLDivElement>();
let navigationVisible = $state(false);
onMount(() => {
if (!navigation) {
return;
}
const observer = new IntersectionObserver((
[entry]
) => { navigationVisible = entry.intersectionRatio === 1; }, { threshold: 1 });
observer.observe(navigation);
return () => observer.disconnect();
});
let container = $state<HTMLDivElement>();
let current = $state('');
</script>
<style>
output {
display: block;
margin-top: 16px;
}
</style>
<div bind:this={navigation}>
<Anchor
affix={false}
showInkInFixed
getCurrentAnchor={navigationVisible ? undefined : () => ''}
direction="horizontal"
getContainer={() => container!}
targetOffset={8}
items={([
{ key: 'details', href: '#anchor-demo-details', title: "描述" },
{ key: 'delivery', href: '#anchor-demo-delivery', title: "送货" },
{ key: 'payment', href: '#anchor-demo-payment', title: "支付" }
])}
onClick={event => event.preventDefault()}
onChange={value => { current = value; }}
/>
</div>
<div
bind:this={container}
style="
height: 180px;
overflow: auto;
margin-top: 12px;
background: var(--ant-color-fill-quaternary);
"
>
<div id="anchor-demo-details" style="
height: 180px;
padding: 16px;
box-sizing: border-box;
">
{("订单说明")}
</div>
<div id="anchor-demo-delivery" style="
height: 180px;
padding: 16px;
box-sizing: border-box;
">
{("交货条款")}
</div>
<div id="anchor-demo-payment" style="
height: 220px;
padding: 16px;
box-sizing: border-box;
">
{("付款方式")}
</div>
</div>
<output aria-label={("当前部分锚点")}>{current}</output>
API
这些示例是根据当前的 ant.design 页面和已安装包的公共 API 进行检查的。 Props 的完整定义位于 @woodger/antd 中。
| 主题 | 示例或限制 |
|---|---|
| Basic | 垂直列表和嵌套链接;单独紧固。 |
| Horizontal Anchor | 平面水平菜单。 |
| Static Anchor | affix=false 和指标管理。 |
| Customize the onClick event | 取消 URL 更改而不取消滚动。 |
| Customize the anchor highlight | getCurrentAnchor 和突出显示的状态。 |
| Set Anchor scroll offset | 通用和单独的目标缩进。 |
| Listening for anchor link change | 历史 onChange。 |
| Replace href in history | replaceState 和 pushState。 |
| Custom semantic dom styling | classNames 对象和 styles 函数。 |
| CSS prefix | 本地锚点、继承链接和独立的 item.prefixCls。 |
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | 完整的本地 CSS 前缀。空字符串和 undefined 共享相同的提供者前缀。 | string | ConfigProvider.prefixCls + "-anchor" |
items | 链接;children 只能用 vertical 方向筑巢。 items 优先于 children-snippet。 | readonly AnchorItem[] | — |
direction | 菜单方向;在 horizontal 方向上,不显示嵌套 children。 | 'vertical' | 'horizontal' | vertical |
affix | 通过 Affix 连接;false 叶锚定在螺纹中。 | boolean | Omit<AffixProps, ...> | true |
showInkInFixed | 当 affix=false 时显示指示器。 | boolean | false |
getContainer | 跟踪和滚动区域; getter 在浏览器中被调用。 | () => HTMLElement | Window | () => window |
offsetTop | 锚点缩进和后备目标缩进。 | number | 0 |
targetOffset | 目标偏移;单个链接的 targetOffset 优先。 | number | offsetTop |
bounds | 活动部分定义公差(以 px 为单位)。 | number | 5 |
getCurrentAnchor | 覆盖突出显示的链接; onChange 获取原始 href。 | (activeLink: string) => string | — |
onClick | preventDefault 防止写入 URL;锚点滚动被保留。 | (event, { title, href }) => void | — |
onChange | 单击或滚动时选定的部分。 | (href: string) => void | — |
replace | 替换历史记录;优先 replace 单个项目。 | boolean | false |
classNames | 语义槽类。 | AnchorSlots | (info) => AnchorSlots | — |
styles | 语义槽 CSS 字符串。 | AnchorSlots | (info) => AnchorSlots | — |
nativeElement | bind:nativeElement 返回外部 div 锚点。 | HTMLDivElement | — |
链接选项
AnchorItem 设置 key、href、title、target、class、replace、targetOffset、prefixCls 和 children。title 接受文本、数字或片段。
item.prefixCls: undefined 继承允许的Anchor前缀;空字符串选择一个公共提供商前缀,后缀为 -anchor;非空字符串指定完整链接前缀。要显式覆盖项目,您需要自定义 CSS。语义回调 Anchor 接收原始本地 props.prefixCls。
ConfigProvider.anchor
AnchorConfig: classstyleclassNamesstyles。class/style - 字符串; classNames/styles - 具有当前属性的插槽对象或函数。
课程加起来。根样式的优先级:defaults.styles.root → defaults.style → 本地 styles.root → 本地 style。其余插槽的本地样式将覆盖默认样式。嵌套锚点替换整个记录,空对象重置设置。更改语义 styles 时,将使用当前链接重新计算指示器的位置。
Semantic DOM
Anchor 创建一个外部 div 和内部 a 具有真实 href 的链接。语义槽: root, item, itemTitle, indicator。styles 接受 CSS 字符串;函数接收当前的 props。一般默认值是通过 ConfigProvider.anchor 设置的。
键盘使用常规的 Tab 和 Enter 链接。当 horizontal 时,菜单会跟随活动链接滚动。装饰指示器;该组件不会为活动链接分配单独的 aria-current。
注册令牌
两个组件令牌都通过以下方式工作 ConfigProvider theme.components.Anchor。该卡显示本地填充;整体颜色和版式遵循该部分的主题。
| 代币 | 目的 |
|---|---|
linkPaddingBlock | 链接的垂直缩进。 |
linkPaddingInlineStart | 嵌套级别缩进。 |
| 代币 | 当前值 |
|---|---|
colorPrimary | #2f54eb |
colorSplit | rgba(5,5,5,0.06) |
colorText | rgba(0,0,0,0.88) |
fontFamily | -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
'Noto Color Emoji' |
fontSize | 14 |
lineHeight | 1.5714285714285714 |
lineType | solid |
lineWidth | 1 |
lineWidthBold | 2 |
motionDurationSlow | 0.3s |
paddingXXS | 4 |
FAQ
为什么示例滚动卡片而不是整个页面?
每个 getContainer 返回一个单独的区域。在应用程序中,您可以保留默认窗口或选择您自己的滚动容器。
为什么粘性菜单会超出 target 边界?
target 指定观察区域。固定节点绘制约束需要CSS组合;它保存在固定示例中。
如何撤消 URL 更改?
在 onClick 中调用 preventDefault。滚动和活动链接检测将继续工作。
为什么 :target 设计不起作用?
历史以编程方式改变。活动部分的状态可通过 onChange 和 getCurrentAnchor 获得。
是否可以制作嵌套的水平列表?
horizontal 不显示 children。对于层次结构,请使用 vertical。