2016-07-10 08:56:02 +08:00
---
category: Components
2016-09-21 11:28:38 +08:00
title: Affix
2020-06-05 13:50:09 +08:00
cover: https://gw.alipayobjects.com/zos/alicdn/tX6-md4H6/Affix.svg
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
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
2021-07-19 16:21:09 +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 / 2xyj5zr85p >
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)