Ant Design Svelte

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.

Scroll to normal button position.

Fixing from below

offsetBottom keeps the action at the bottom border while its normal position is below the visible area.

No events

Change of state

onChange communicates the transition between thread and pinning. The example state and event history belong to the application.

In the stream

Selecting a scroll container

target returns HTMLElement. External page scrolling does not change local anchoring; buttons control the selected area.

In the stream

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.

In the stream In the stream

General Affix settings

ConfigProvider.affix, general scroll target, local overrides and zIndexPopup.

Scroll the container: the button will be anchored at its top border.

In the stream

Project action

Saved example of a review with local pinning and a click counter.

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.

Topics of official examples
ThemeExample or limitation
BasicUpper limit; a separate card shows offsetBottom.
CallbackState changes and history.
Container to scroll.Own target and page scrolling.
Main parameters of Affix
PropertyDescriptionTypeDefault
offsetTopIndent from the top edge of the target in px; the condition of pinning from above is checked first.number0 if offsetBottom is not specified
offsetBottomIndent from the bottom edge of the target in px.number—
targetLocal target has priority. null means the area is not yet ready; getter is not called with SSR.() => Window | HTMLElement | nullConfigProvider.getTargetContainer / window
onChangeDock state transition.(affixed?: boolean) => void—
childrenContent that is saved with a placeholder.Snippet—
classExternal container class.string—
rootClassNameClass of internal node only in docked state.string—
styleOuter container CSS; does not directly change the position of the internal node.string—
prefixClsFull 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.stringConfigProvider + -affix
nativeElementbind:nativeElement returns the outer container.HTMLDivElement—

ConfigProvider.affix

PropertyDescriptionTypeDefault
classExternal node class; the local class is added to the general one.string—
styleCSS 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.

Affix Component Tokens
TokenPurpose
zIndexPopupLayer 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.