2016-07-10 08:56:02 +08:00
---
category: Components
2016-09-21 11:28:38 +08:00
title: Affix
2022-11-30 20:14:41 +08:00
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*YSm4RI3iOJ8AAAAAAAAAAAAADrJ8AQ/original
2023-02-09 22:17:31 +08:00
coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*03dxS64LxeQAAAAAAAAAAAAADrJ8AQ/original
2022-11-09 12:28:04 +08:00
demo:
cols: 2
group:
2022-11-30 15:57:10 +08:00
title: Other
2022-11-17 18:28:13 +08:00
order: 7
2016-07-10 08:56:02 +08:00
---
2019-10-28 09:09:22 +08:00
Wrap Affix around another component to make it stick the viewport.
2016-07-10 08:56:02 +08:00
2016-09-10 13:43:30 +08:00
## When To Use
2016-07-10 08:56:02 +08:00
2021-01-27 16:45:25 +08:00
On longer web pages, it's helpful to stick component into the viewport. This is common for menus and actions.
2016-07-10 08:56:02 +08:00
2016-11-29 17:13:24 +08:00
Please note that Affix should not cover other content on the page, especially when the size of the viewport is small.
2016-07-10 08:56:02 +08:00
2022-11-09 12:28:04 +08:00
## Examples
2022-11-17 17:31:26 +08:00
<!-- prettier - ignore -->
2022-11-09 12:28:04 +08:00
< code src = "./demo/basic.tsx" > Basic< / code >
< code src = "./demo/on-change.tsx" > Callback< / code >
< code src = "./demo/target.tsx" > Container to scroll.< / code >
< code src = "./demo/debug.tsx" debug > debug< / code >
2016-07-10 08:56:02 +08:00
## API
2023-08-08 18:27:48 +08:00
Common props ref: [Common props](/docs/react/common-props)
2019-11-21 10:24:22 +08:00
| Property | Description | Type | Default |
| --- | --- | --- | --- |
| offsetBottom | Offset from the bottom of the viewport (in pixels) | number | - |
| offsetTop | Offset from the top of the viewport (in pixels) | number | 0 |
| target | Specifies the scrollable area DOM node | () => HTMLElement | () => window |
2021-10-25 19:43:17 +08:00
| onChange | Callback for when Affix state is changed | (affixed?: boolean) => void | - |
2016-09-10 12:45:33 +08:00
2019-10-28 09:09:22 +08:00
**Note:** Children of `Affix` must not have the property `position: absolute` , but you can set `position: absolute` on `Affix` itself:
2016-09-10 12:45:33 +08:00
```jsx
2019-05-07 14:57:32 +08:00
< Affix style = {{ position: ' absolute ' , top: y , left: x } } > ...< / Affix >
2016-09-10 12:45:33 +08:00
```
2019-04-17 10:19:57 +08:00
## FAQ
2021-07-19 16:21:09 +08:00
### When binding container with `target` in Affix, elements sometimes move out of the container.
2019-04-17 10:19:57 +08:00
2023-05-28 12:01:42 +08:00
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 >
2019-04-17 10:19:57 +08:00
2019-10-28 09:09:22 +08:00
Related issues: [#3938](https://github.com/ant-design/ant-design/issues/3938) [#5642 ](https://github.com/ant-design/ant-design/issues/5642 ) [#16120 ](https://github.com/ant-design/ant-design/issues/16120 )
2021-05-31 12:17:58 +08:00
### When Affix is used in a horizontal scroll container, the position of the element `left` is incorrect.
2021-07-19 16:21:09 +08:00
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.
2021-05-31 12:17:58 +08:00
Related issues: [#29108](https://github.com/ant-design/ant-design/issues/29108)