Affix
Attach an action to the edge of a window or selected scroll container.
When to use
Affix keeps the action or toolbar at the top or bottom edge when scrolling vertically. It is suitable for a block that first participates in the normal document flow and is pinned after reaching a threshold.
For constant floating action use FloatButton. For simple anchoring within an area, native CSS position:sticky may be suitable.
Observation area and indents
target returns Window or HTMLElement. Local getter takes precedence over ConfigProvider.getTargetContainer; without both, window. Returning null means waiting for the area to be ready.
| Problem | Setting | Example |
|---|---|---|
| Secure at the top edge | offsetTop |
Securing from above |
| Secure at the bottom edge | offsetBottom |
Fastening from below |
| Read state change | onChange |
State change |
| Watch local scrolling | target |
Selecting a scroll container |
| Inherit shared settings | ConfigProvider.affix, getTargetContainer |
Affix General Settings |
Without a specified direction, a top edge with zero padding is used. When offsetTop and offsetBottom are set at the same time, the top condition is checked first. For clear behavior, choose one edge.
onChange reports a transition between the sticky state and the normal thread, rather than every scroll event. children - Svelte snippet of content. Leave it in normal flow so Affix can measure the dimensions and save the block space.
Container boundaries and recalculation
target sets the observation area, but does not limit the drawing of a fixed node to its boundaries. When scrolling the page, the local target may move relative to the viewport. This behavior is discussed separately in the official FAQ.
Examples create a custom containing block, clipping, and local CSS indentation for a pinned node. This design refers to the demonstration; When migrating the example, keep the associated CSS wrapper. target alone is not enough to automatically limit a button to a card.
Once the adjacent DOM has changed, the dimension can be queried via updatePosition(). The method is accessible via bind:this and schedules work on the animation frame; updatePosition.cancel() cancels the pending call. Wait for the Svelte tick after resizing as in example of recalculation.
Affix is designed for a vertical scenario. When scrolling horizontally, do not count on universal tracking of the left coordinate. For such areas, consider a separate layout or CSS sticky.
Design and availability
class/style refers to the external node, rootClassName refers to the pinned node. ConfigProvider.affix inherits class/style; the passed nested record replaces the entire parent record. General settings show two independent regions.
bind:nativeElement returns the outer div after mount. Getter target is executed in the browser; get the DOM via bind:this and return null until the node is ready. The pin itself does not change the semantics of the nested button or link: set the accessible name, disabled and handlers to the content.
Affix does not have separate component design tokens or semantic classNames/styles. The padding specifies the docking position, and the size and styling of the action remain properties of the child component.
prefixCls specifies the full local CSS prefix of the pinned node. The empty string and undefined inherit ConfigProvider.prefixCls with the suffix -affix, defaulting to ant-affix. Changing the prefix does not change the target, offset and DOM of the pinned content. In SSR, the Affix remains in the thread and does not receive an affix class. Example ConfigProvider checks for an active Affix inside an isolated scroll area.
Examples
Fixing from above
offsetTop changes the distance to the upper boundary of the target. The example is isolated in its own scroll container.
<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>{("Top indent ")}<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={("Scroll from top Affix")} tabindex="0">
<div style="height:48px"></div>
<Affix
rootClassName="demo-affix-fixed"
target={() => area ?? null}
offsetTop={offset}
><Button
type="primary"
onclick={() => { clicks += 1; }}
>{("Action from above (")}{clicks})</Button></Affix>
<div style="height:360px"></div>
</div>
</div>
Fixing from below
offsetBottom keeps the action at the bottom border while its normal position is below the visible area.
<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={("Scroll from bottom Affix")} tabindex="0">
<div style="
height: 260px;
padding: 12px;
">
{("Scroll to normal button position.")}
</div>
<Affix
rootClassName="demo-affix-fixed"
target={() => area ?? null}
offsetBottom={offset}
><Button
type="primary"
onclick={() => { clicks += 1; }}
>{("Action from below (")}{clicks})</Button></Affix>
<div style="height:60px"></div>
</div>
</div>
Change of state
onChange communicates the transition between thread and pinning. The example state and event history belong to the application.
<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 ? "Fixed" : "In the stream"];
}
</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; }}
>{("Toggle scrolling")}</Button></div>
<div class="frame" style:--demo-affix-offset={("" + String((offset)) + "px")}>
<div class="scroll" bind:this={area} role="region" aria-label={("Scrolling Events Affix")} tabindex="0">
<div style="height:48px"></div>
<Affix
rootClassName="demo-affix-fixed"
target={() => area ?? null}
offsetTop={offset}
onChange={change}
><Button>{("Action bar")}</Button></Affix>
<div style="height:360px"></div>
</div>
</div>
<output aria-label={("Events Affix")}>{(events.join(' → ')
|| ("No events"))}</output>
Selecting a scroll container
target returns HTMLElement. External page scrolling does not change local anchoring; buttons control the selected area.
<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; }}>
{("Scroll container")}
</Button><Button onclick={() => {
if (area) area.scrollTop = 0;
}}>{("Return to start")}</Button></div>
<div class="frame" style:--demo-affix-offset={("" + String((offset)) + "px")}>
<div class="scroll" bind:this={area} role="region" aria-label={("Affix target container")} tabindex="0">
<div style="height:56px"></div>
<Affix
rootClassName="demo-affix-fixed"
target={() => area ?? null}
offsetTop={offset}
onChange={next => { affixed = Boolean(next); }}
><Button>{("Local action")}</Button></Affix>
<div style="height:360px"></div>
</div>
</div>
<output aria-label={("Securing the Affix container")}>{(affixed ? "Fixed" : "In the stream")}</output>
Recalculation after document changes
updatePosition via bind:this recalculates the position after changing the DOM and tick. Automatic scroll anchoring is disabled in this card so that the change in position is visible.
<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; }}>
{("Fix before recalculation")}
</Button><Button onclick={addSpace}>
{("Add space on top")}
</Button></div>
<div class="frame" style:--demo-affix-offset={("" + String((offset)) + "px")}>
<div class="scroll" bind:this={area} role="region" aria-label={("Affix conversion container")} 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>{("Recalculated action")}</Button></Affix>
<div style="height:360px"></div>
</div>
</div>
<output aria-label={("Recalculation Affix")}>{(affixed ? "Fixed" : "In the stream")}</output>
General Affix settings
ConfigProvider.affix, general scroll target, local overrides and 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; }}
>{("General area")}</Button>
<Button onclick={() => { if (localArea) localArea.scrollTop = 100; }}>
{("Local area")}
</Button>
<Button onclick={reset}>{("Reset areas")}</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={("Common Affix Container")}
tabindex="0">
<div style="height: 48px"></div>
<Affix offsetTop={12} rootClassName="provider-demo-affix"
onChange={next => { commonAffixed = Boolean(next); }}>
<Button>{("General target")}</Button>
</Affix>
<div style="height: 320px"></div>
</div>
</div>
<div class="frame">
<div class="scroll" bind:this={localArea} role="region" aria-label={("Local Affix container")}
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>{("Local target")}</Button>
</Affix>
<div style="height: 320px"></div>
</div>
</div>
</div>
</ConfigProvider>
<output aria-label={("General target Affix")}>{(commonAffixed ? "Fixed" : "In the stream")}</output>
<output aria-label={("Local target Affix")}>{(localAffixed ? "Fixed" : "In the stream")}</output>
Scroll the container: the button will be anchored at its top border.
Project action
Saved example of a review with local pinning and a click counter.
<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>{("Scroll the container: the button will be anchored at its top border.")}</p>
<div class="affix-frame" style:--affix-offset={("" + String((offsetTop)) + "px")}>
<div
bind:this={container}
role="region"
aria-label={("Affix scroll area")}
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++; }}
>{("Action (")}{clicks})</Button>
</Affix>
<div style="height:360px"></div>
</div>
</div>
<output aria-label={("Status Affix")}>{(affixed ? "Fixed" : "In the stream")}</output>
API
Themes of the ant.design live page are mapped to the public API of the installed package. Full definitions of Props are in @woodger/antd.
| Theme | Example or limitation |
|---|---|
| Basic | Upper limit; a separate card shows offsetBottom. |
| Callback | State changes and history. |
| Container to scroll. | Own target and page scrolling. |
| Property | Description | Type | Default |
|---|---|---|---|
offsetTop | Indent from the top edge of the target in px; the condition of pinning from above is checked first. | number | 0 if offsetBottom is not specified |
offsetBottom | Indent from the bottom edge of the target in px. | number | — |
target | Local target has priority. null means the area is not yet ready; getter is not called with SSR. | () => Window | HTMLElement | null | ConfigProvider.getTargetContainer / window |
onChange | Dock state transition. | (affixed?: boolean) => void | — |
children | Content that is saved with a placeholder. | Snippet | — |
class | External container class. | string | — |
rootClassName | Class of internal node only in docked state. | string | — |
style | Outer container CSS; does not directly change the position of the internal node. | string | — |
prefixCls | Full local CSS prefix of the pinned node. The empty string and undefined inherit ConfigProvider.prefixCls; the default is ant-affix. target and fixture geometry are preserved. | string | ConfigProvider + -affix |
nativeElement | bind:nativeElement returns the outer container. | HTMLDivElement | — |
ConfigProvider.affix
| Property | Description | Type | Default |
|---|---|---|---|
class | External node class; the local class is added to the general one. | string | — |
style | CSS of the outer node before the local style. A nested affix record replaces the entire parent one. | string | — |
Semantic DOM
The outer div maintains its position in the flow; The inner div gets position:fixed when docked. placeholder stores the measured width/height. class/style refers to the outer div, rootClassName refers to the inner anchor node. ConfigProvider.affix sets the class/style of the external node, local props specify them. There are no separate semantic classNames/styles.
Affix does not change the semantics of the content and does not create an additional Tab-stop. Select Buttons, links, and other available elements based on their purpose.
In the examples, transform creates a local containing block, and scoped CSS through rootClassName translates the top/bottom calculated from the window into a local offset. This is gallery isolation; It is not needed for regular docking in viewport.
Registration tokens
theme.components.Affix.zIndexPopup controls the pinned node layer. In the general settings example, the value is set to 17. rootClassName and custom CSS allow you to refine the styling; consider the stacking contexts of the parent.
| Token | Purpose |
|---|---|
zIndexPopup | Layer order of the pinned element. |
FAQ
How is the scroll target inherited?
Local target takes precedence over ConfigProvider.getTargetContainer; without them, window. null from a local getter means waiting for the node to be ready.
Why does Affix go beyond the target boundary when scrolling the page?
target specifies the area to be viewed, and the fixed position is associated with the viewport. To constrain the drawing, you need the appropriate CSS composition shown in the examples.
Is horizontal scrolling supported?
Affix is designed for a vertical scenario. For the horizontal area, consider CSS position:sticky or a separate layout.
How to request a recount?
After changing the DOM, wait for the tick and call updatePosition() via bind:this. The pending call is canceled via updatePosition.cancel().
Is it possible to set two indents at once?
First, offsetTop is checked, then offsetBottom. For unambiguous direction, pass one indent.