Affix
将操作附加到窗口或选定滚动容器的边缘。
何时使用
垂直滚动时,Affix 将操作或工具栏保持在顶部或底部边缘。它适用于首先参与正常文档流并在达到阈值后被固定的块。
对于恒定浮动动作,请使用 FloatButton。对于区域内的简单锚定,本机 CSS position:sticky 可能适合。
观察区域和边缘
target 返回 Window 或 HTMLElement。本地 getter 优先于 ConfigProvider.getTargetContainer;没有两者,窗口。返回 null 意味着等待该区域准备好。
| 问题 | 设置 | 示例 |
|---|---|---|
| 固定在顶部边缘 | offsetTop |
从上方固定 |
| 固定在底部边缘 | offsetBottom |
从下方紧固 |
| 读取状态变化 | onChange |
状态变化 |
| 观看本地滚动 | target |
选择滚动容器 |
| 继承共享设置 | ConfigProvider.affix、getTargetContainer |
附加常规设置 |
如果没有指定方向,则使用带有零填充的顶部边缘。当同时设置 offsetTop 和 offsetBottom 时,首先检查顶部条件。为了获得清晰的行为,请选择一条边。
onChange 报告粘滞状态和普通线程之间的转换,而不是每个 scroll 事件。children - 内容的精简片段。使其保持正常流动,以便 Affix 可以测量尺寸并节省块空间。
容器边界和重新计算
target 设置观察区域,但不将固定节点的绘制限制在其边界内。滚动页面时,本地 target 可能会相对于视口移动。此行为在官方常见问题解答中单独讨论。
这些示例为锚定节点创建自定义包含 block、修剪和本地 CSS 缩进。本设计参考演示;迁移示例时,保留关联的 CSS 包装器。仅 target 不足以自动将按钮限制为卡片。
一旦相邻的 DOM 发生变化,就可以通过 updatePosition() 查询维度。该方法可通过 bind:this 访问并安排动画帧上的工作; updatePosition.cancel() 取消挂起的呼叫。调整大小后等待 Svelte 勾选,如 重新计算的示例 中所示。
Affix 是针对垂直场景而设计的。水平滚动时,不要指望左坐标的通用跟踪。对于此类区域,请考虑单独的布局或 CSS 粘性。
设计和可用性
class/style 指的是外部节点,rootClassName 指的是固定节点。 ConfigProvider.affix继承 class/style;传递的嵌套记录将替换整个父记录。 常规设置 显示两个独立区域。
bind:nativeElement 在挂载后返回外部 div。 Gettertarget 在浏览器中执行;通过 bind:this 获取DOM并返回 null,直到节点准备好。引脚本身不会更改嵌套按钮或链接的语义:将可访问名称、disabled 和处理程序设置为内容。
Affix 没有单独的组件设计令牌(design tokens)或语义 classNames/styles。填充指定停靠位置,而操作的大小和样式仍然是子组件的属性。
prefixCls 指定固定节点的完整本地 CSS 前缀。空字符串和 undefined 继承ConfigProvider.prefixCls,后缀为 -affix,默认为 ant-affix。更改前缀不会更改固定内容的 target、offset 和 DOM。在SSR中,词缀保留在线程中并且不接收词缀类。 示例 ConfigProvider 检查隔离滚动区域内的活动词缀。
示例
从上方固定
offsetTop 更改到 target 上边框的距离。该示例被隔离在其自己的滚动容器中。
<script lang="ts">
import { Affix, Button } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let offset = $state(12);
let clicks = $state(0);
</script>
<style>
.frame {
transform: translate(0);
isolation: isolate;
overflow: hidden;
border-radius: 8px;
background: var(--ant-color-fill-quaternary);
}
.scroll {
height: 180px;
overflow: auto;
}
.frame :global(.demo-affix-fixed) { top: var(--demo-affix-offset) !important; }
.controls {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
label {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
</style>
<div
class="controls"
><label>{("上边距 ")}<input
type="range"
min="0"
max="32"
bind:value={offset}
/><output>{offset}px</output></label></div>
<div class="frame" style:--demo-affix-offset={("" + String((offset)) + "px")}>
<div class="scroll" bind:this={area} role="region" aria-label={("从顶部滚动 Affix")} tabindex="0">
<div style="height:48px"></div>
<Affix
rootClassName="demo-affix-fixed"
target={() => area ?? null}
offsetTop={offset}
><Button
type="primary"
onclick={() => { clicks += 1; }}
>{("上面的动作(")}{clicks})</Button></Affix>
<div style="height:360px"></div>
</div>
</div>
从下方紧固
offsetBottom 将动作保持在底部边框,而其正常位置位于可见区域下方。
<script lang="ts">
import { Affix, Button } from '@woodger/antd';
let area = $state<HTMLDivElement>();
const offset = 12;
let clicks = $state(0);
</script>
<style>
.frame {
transform: translate(0);
isolation: isolate;
overflow: hidden;
border-radius: 8px;
background: var(--ant-color-fill-quaternary);
}
.scroll {
height: 180px;
overflow: auto;
}
.frame :global(.demo-affix-fixed) { bottom: var(--demo-affix-offset) !important; }
</style>
<div class="frame" style:--demo-affix-offset={("" + String((offset)) + "px")}>
<div class="scroll" bind:this={area} role="region" aria-label={("从底部滚动 Affix")} tabindex="0">
<div style="
height: 260px;
padding: 12px;
">
{("滚动到正常按钮位置。")}
</div>
<Affix
rootClassName="demo-affix-fixed"
target={() => area ?? null}
offsetBottom={offset}
><Button
type="primary"
onclick={() => { clicks += 1; }}
>{("从下面开始行动(")}{clicks})</Button></Affix>
<div style="height:60px"></div>
</div>
</div>
状态变化
onChange 传达线程和粘性之间的转换。示例状态和事件历史记录属于应用程序。
<script lang="ts">
import { Affix, Button } from '@woodger/antd';
let area = $state<HTMLDivElement>();
const offset = 8;
let events = $state<string[]>([]);
let affixed = $state(false);
function change(next?: boolean) {
affixed = Boolean(next);
events = [...events, affixed ? "已固定" : "在流动中"];
}
</script>
<style>
.frame {
transform: translate(0);
isolation: isolate;
overflow: hidden;
border-radius: 8px;
background: var(--ant-color-fill-quaternary);
}
.scroll {
height: 180px;
overflow: auto;
}
.frame :global(.demo-affix-fixed) { top: var(--demo-affix-offset) !important; }
.controls {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
output {
display: block;
margin-top: 16px;
}
</style>
<div
class="controls"
><Button
onclick={() => { if (area) area.scrollTop = affixed ? 0 : 90; }}
>{("切换滚动")}</Button></div>
<div class="frame" style:--demo-affix-offset={("" + String((offset)) + "px")}>
<div class="scroll" bind:this={area} role="region" aria-label={("滚动词缀事件")} tabindex="0">
<div style="height:48px"></div>
<Affix
rootClassName="demo-affix-fixed"
target={() => area ?? null}
offsetTop={offset}
onChange={change}
><Button>{("操作栏")}</Button></Affix>
<div style="height:360px"></div>
</div>
</div>
<output aria-label={("附加事件")}>{(events.join(' → ')
|| ("没有活动"))}</output>
选择滚动容器
target 返回 HTMLElement。外部页面滚动不会改变本地锚定;按钮控制所选区域。
<script lang="ts">
import { Affix, Button } from '@woodger/antd';
let area = $state<HTMLDivElement>();
const offset = 16;
let affixed = $state(false);
</script>
<style>
.frame {
transform: translate(0);
isolation: isolate;
overflow: hidden;
border-radius: 8px;
background: var(--ant-color-fill-quaternary);
}
.scroll {
height: 180px;
overflow: auto;
}
.frame :global(.demo-affix-fixed) { top: var(--demo-affix-offset) !important; }
.controls {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
output {
display: block;
margin-top: 16px;
}
</style>
<div class="controls"><Button onclick={() => { if (area) area.scrollTop = 100; }}>
{("滚动容器")}
</Button><Button onclick={() => {
if (area) area.scrollTop = 0;
}}>{("回到最初")}</Button></div>
<div class="frame" style:--demo-affix-offset={("" + String((offset)) + "px")}>
<div class="scroll" bind:this={area} role="region" aria-label={("附加目标容器")} tabindex="0">
<div style="height:56px"></div>
<Affix
rootClassName="demo-affix-fixed"
target={() => area ?? null}
offsetTop={offset}
onChange={next => { affixed = Boolean(next); }}
><Button>{("当地行动")}</Button></Affix>
<div style="height:360px"></div>
</div>
</div>
<output aria-label={("固定容器附件")}>{(affixed ? "已固定" : "在流动中")}</output>
文件变更后重新计算
通过 bind:this 调用 updatePosition,在 DOM 更新并执行 tick 后重新计算位置。此卡中禁用自动 scroll 锚定,以便位置的变化可见。
<script lang="ts">
import { tick } from 'svelte';
import { Affix, Button, type AffixUpdatePosition } from '@woodger/antd';
let area = $state<HTMLDivElement>();
const offset = 8;
let instance = $state<{ updatePosition: AffixUpdatePosition }>();
let spacer = $state(48);
let affixed = $state(false);
async function addSpace() {
spacer += 80;
await tick();
instance?.updatePosition();
}
</script>
<style>
.frame {
transform: translate(0);
isolation: isolate;
overflow: hidden;
border-radius: 8px;
background: var(--ant-color-fill-quaternary);
}
.scroll {
height: 180px;
overflow: auto;
overflow-anchor: none;
}
.frame :global(.demo-affix-fixed) { top: var(--demo-affix-offset) !important; }
.controls {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
output {
display: block;
margin-top: 16px;
}
</style>
<div class="controls"><Button onclick={() => { if (area) area.scrollTop = 90; }}>
{("重新计算前修复")}
</Button><Button onclick={addSpace}>
{("在顶部添加空间")}
</Button></div>
<div class="frame" style:--demo-affix-offset={("" + String((offset)) + "px")}>
<div class="scroll" bind:this={area} role="region" aria-label={("粘贴转换容器")} tabindex="0">
<div style={`height:${spacer}px`}></div>
<Affix
bind:this={instance}
rootClassName="demo-affix-fixed"
target={() => area ?? null}
offsetTop={offset}
onChange={next => { affixed = Boolean(next); }}
><Button>{("重新计算的行动")}</Button></Affix>
<div style="height:360px"></div>
</div>
</div>
<output aria-label={("附加重新计算")}>{(affixed ? "已固定" : "在流动中")}</output>
附加常规设置
ConfigProvider.affix、常规 scroll target、本地覆盖和 zIndexPopup。
<script lang="ts">
import { Affix, Button, ConfigProvider } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let localArea = $state<HTMLDivElement>();
let commonAffixed = $state(false);
let localAffixed = $state(false);
function reset() {
if (area) area.scrollTop = 0;
if (localArea) localArea.scrollTop = 0;
}
</script>
<style>
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
.areas {
display: grid;
gap: 16px;
}
.frame {
transform: translate(0);
isolation: isolate;
overflow: hidden;
border-radius: 8px;
background: var(--ant-color-fill-quaternary);
}
.scroll {
height: 160px;
overflow: auto;
}
.frame :global(.provider-demo-affix) { top: 12px !important; }
output {
display: block;
margin-top: 12px;
}
</style>
<div class="controls">
<Button
onclick={() => { if (area) area.scrollTop = 100; }}
>{("一般区")}</Button>
<Button onclick={() => { if (localArea) localArea.scrollTop = 100; }}>
{("当地")}
</Button>
<Button onclick={reset}>{("重置区域")}</Button>
</div>
<ConfigProvider
getTargetContainer={() => area ?? window}
affix={{
class: 'affix-provider-root',
style: `
outline: 1px dashed #d48806;
padding-inline: 8px;
`
}}
theme={{ components: { Affix: { zIndexPopup: 17 } } }}
>
<div class="areas">
<div class="frame">
<div class="scroll" bind:this={area} role="region" aria-label={("通用 Affix 容器")}
tabindex="0">
<div style="height: 48px"></div>
<Affix offsetTop={12} rootClassName="provider-demo-affix"
onChange={next => { commonAffixed = Boolean(next); }}>
<Button>{("总体目标")}</Button>
</Affix>
<div style="height: 320px"></div>
</div>
</div>
<div class="frame">
<div class="scroll" bind:this={localArea} role="region" aria-label={("本地 Affix 容器")}
tabindex="0">
<div style="height: 48px"></div>
<Affix target={() => localArea ?? null} offsetTop={12} rootClassName="provider-demo-affix"
class="affix-local-root" style="
outline-color: #389e0d;
padding-inline: 4px;
"
onChange={next => { localAffixed = Boolean(next); }}>
<Button>{("本地目标")}</Button>
</Affix>
<div style="height: 320px"></div>
</div>
</div>
</div>
</ConfigProvider>
<output aria-label={("一般目标词缀")}>{(commonAffixed ? "已固定" : "在流动中")}</output>
<output aria-label={("本地目标词缀")}>{(localAffixed ? "已固定" : "在流动中")}</output>
滚动容器:按钮将固定在其顶部边框。
项目行动
使用本地固定和点击计数器保存的评论示例。
<script lang="ts">
import { Affix, Button } from '@woodger/antd';
const offsetTop = 8;
let container = $state<HTMLDivElement>();
let affixed = $state(false);
let clicks = $state(0);
</script>
<style>
output {
display: block;
margin-top: 16px;
}
.affix-frame { transform: translate(0); }
.affix-frame :global(.showcase-affix-fixed) { top: var(--affix-offset) !important; }
</style>
<p>{("滚动容器:按钮将固定在其顶部边框。")}</p>
<div class="affix-frame" style:--affix-offset={("" + String((offsetTop)) + "px")}>
<div
bind:this={container}
role="region"
aria-label={("附加滚动区域")}
style="
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
"
>
<div style="height:48px"></div>
<Affix
rootClassName="showcase-affix-fixed"
target={() => container ?? null}
{offsetTop}
onChange={value => { affixed = Boolean(value); }}
>
<Button
type="primary"
onclick={() => { clicks++; }}
>{("行动 (")}{clicks})</Button>
</Affix>
<div style="height:360px"></div>
</div>
</div>
<output aria-label={("附加状态")}>{(affixed ? "已固定" : "在流动中")}</output>
API
ant.design 实时页面主题映射到已安装包的公共 API。 Props 的完整定义位于 @woodger/antd 中。
| 主题 | 示例或限制 |
|---|---|
| Basic | 上限;单独的卡片显示 offsetBottom。 |
| Callback | 国家变迁和历史。 |
| Container to scroll. | 自己的 target 和页面滚动。 |
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
offsetTop | 从 target 顶部边缘缩进(以 px 为单位);首先检查上方的固定情况。 | number | 如果未指定 offsetBottom,则为 0 |
offsetBottom | 从 target 底部边缘缩进(以 px 为单位)。 | number | — |
target | 本地 target 优先。 null 表示该区域尚未准备好; getter 不通过 SSR 调用。 | () => Window | HTMLElement | null | ConfigProvider.getTargetContainer / window |
onChange | 停靠状态转换。 | (affixed?: boolean) => void | — |
children | 使用 placeholder 保存的内容。 | Snippet | — |
class | 外容器类。 | string | — |
rootClassName | 仅处于固定状态的内部节点类。 | string | — |
style | 外容器CSS;不直接改变内部节点的位置。 | string | — |
prefixCls | 固定节点的完整本地 CSS 前缀。空字符串和 undefined 继承ConfigProvider.prefixCls;默认是 ant-affix。target 和夹具几何形状被保留。 | string | ConfigProvider + -affix |
nativeElement | bind:nativeElement 返回外部容器。 | HTMLDivElement | — |
ConfigProvider.affix
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
class | 外部节点类;本地 class 被添加到通用类中。 | string | — |
style | 外部节点 CSS 位于本地 style 之前。嵌套的词缀记录会替换整个父记录。 | string | — |
Semantic DOM
外部 div 保持其在流中的位置;内部 div 在停靠时获得 position:fixed。placeholder 存储测量的 width/height。 class/style 指的是外层 div,rootClassName 指的是内层锚节点。 ConfigProvider.affix 设置外部节点的 class/style,本地 props 指定它们。没有单独的语义 classNames/styles。
Affix 不会更改内容的语义,也不会创建额外的制表位。根据用途选择按钮、链接和其他可用元素。
在示例中,transform 创建一个本地包含 block,并且作用域 CSS 通过 rootClassName 将从窗口计算出的 top/bottom 转换为本地 offset。这是画廊的隔离;在视口中常规停靠不需要它。
注册令牌
theme.components.Affix.zIndexPopup 控制固定节点层。在常规设置示例中,该值设置为 17。rootClassName 和自定义 CSS 允许您细化样式;考虑父级的堆栈上下文。
| 代币 | 目的 |
|---|---|
zIndexPopup | 固定元素的层顺序。 |
FAQ
如何继承滚动容器的 target?
本地 target 优先于 ConfigProvider.getTargetContainer;没有它们,窗户。来自本地 getter 的 null 意味着等待节点准备就绪。
为什么Affix在滚动页面时会超出 target 边界?
target 指定要查看的区域,固定位置与视口关联。要约束绘图,您需要示例中所示的适当 CSS 组合。
是否支持水平滚动?
Affix 是针对垂直场景而设计的。对于水平区域,请考虑 CSS position:sticky 或单独的布局。
如何请求重新计票?
更改 DOM 后,等待滴答声并通过 bind:this 调用 updatePosition()。通过 updatePosition.cancel() 取消挂起的呼叫。
是否可以同时设置两个缩进?
首先检查 offsetTop,然后检查 offsetBottom。为了明确方向,请通过一个缩进。