JGuang

Affix

Affix · Ant Design

Preview
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Affix

Stick an element to the viewport.

When To Use

On longer web pages, it's helpful to stick component into the viewport. This is common for menus and actions.

Please note that Affix should not cover other content on the page, especially when the size of the viewport is small.

Notes for developers

After version 5.10.0, we rewrite Affix use FC, some methods of obtaining ref and calling internal instance methods will be invalid.

Examples

  • Basic · basic
  • Callback · on-change
  • Container to scroll. · target

API

Common props ref:Common props

PropertyDescriptionTypeDefaultVersionGlobal Config
offsetBottomOffset from the bottom of the viewport (in pixels)number-×
offsetTopOffset from the top of the viewport (in pixels)number0×
targetSpecifies the scrollable area DOM node() => Window | HTMLElement | null() => window×
onChangeCallback for when Affix state is changed(affixed?: boolean) => void-×

Note: Children of Affix must not have the property position: absolute, but you can set position: absolute on Affix itself:

<Affix style={{ position: 'absolute', top: y, left: x }}>...</Affix>

FAQ

When binding container with target in Affix, elements sometimes move out of the container.

We only listen to container scroll events for performance consideration. You can add custom listeners if you still want to: https://codesandbox.io/s/stupefied-maxwell-ophqnm?file=/index.js

Related issues:#3938 #5642 #16120

When Affix is ​​used in a horizontal scroll container, the position of the element left is incorrect.

Affix is ​​generally only applicable to areas with one-way scrolling, and only supports usage in vertical scrolling containers. If you want to use it in a horizontal container, you can consider implementing with the native position: sticky property.

Related issues:#29108

Shared native props

Tips: The following generic properties apply to most antd components; those not supported are described separately.

PropertyDescriptionTypeDefault
styleThe additional styleCSSProperties-
classNameThe additional css classstring-
rootClassNameClassName on the root elementstring-
autoFocusAuto focus when component mounted, only effective for focusable elements like forms, links, etc.booleanfalse

Install and reproduce an example

npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Use ConfigProvider and native App; configure dayjs locale separately. Each example lists its complete source files and additional pinned dependencies in View code. Example names select demos, not universal component props. See the resource setup guide for theme, locale and SSR.

Native type references

The source viewer includes unchanged native implementations and the following source declarations. Inherited types remain in their native packages. English source comments are preserved as code reference.

  • vendor/components/affix/index.tsx
  • vendor/components/affix/style/index.ts
  • vendor/components/affix/utils.ts

Source and license

https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd

Original bilingual prose and executable examples are retained. Website-only embeds and token tables are expanded locally. Ant Design is MIT; separately listed dependency notices also apply. Upload previews block outbound requests and do not provide a backend.

Local example adaptations

  • authored/demos/affix/basic.tsx: Provide independent scroll targets for top/bottom pinning and bounded interactive offsets so viewport-fixed controls cannot overlap normal-flow siblings in an embedded preview. Original reference: vendor/components/affix/demo/basic.tsx.

Copy the adapted sourceFiles listed by the example; upstream reference source remains unchanged. See LOCAL_CHANGES.md.