JGuang

Affix

Affix · Arco Design

Preview
npm install @arco-design/web-react@2.66.18

Affix

Pin the elements to the visible range. When the content area is relatively long and the page needs to scroll, the Affix can fix the element on the screen. Often used for side menu and button group.

Install

npm install @arco-design/web-react@2.66.18

Import @arco-design/web-react/dist/css/arco.css at the entry and set the locale with ConfigProvider. React 19 projects must also import @arco-design/web-react/es/_util/react-19-adapter first, or imperative APIs such as Message and Modal cannot render.

Examples

  • Basic: Basic usage. If the fixed position is not set, the element will be fixed at the top of the page when it scrolls outside the page.
  • Fixed Top: The element will be fixed when it scrolls down to a certain distance from the top of the viewport.
  • Fixed Bottom: The element will be fixed when it scrolls down to a certain distance from the bottom of the viewport.
  • Callback: Callback when the fixed state changes.
  • Container: Use target to set the element whose scroll event needs to be listened to. The default value is window. If target is specified as a non-window container, the fixed element may escape the container when the outer element scrolls. You can pass in targetContainer to set the scrollable element outside target. Affix will listen to the scroll event of the element and update the position of Affix correspondingly. You can also listen to the scroll event of the outer scrollable element and call this.affixRef.updatePosition() to update the position.

Examples come from the official Arco Design repository (MIT) at the commit published as npm 2.66.18. Some use online images or upload endpoints; network requests are disabled in previews.

API

Affix

PropertyDescriptionTypeDefaultValueVersion
offsetBottomOffset from the bottom of the viewport (in pixels)number--
offsetTopOffset from the top of the viewport (in pixels)number0-
affixClassNameClassName of the fixed element.string | string[]-2.8.0
affixStyleStyle of the fixed elements. Don't set position top width height attributes as they are used for positioning when the element is fixed.CSSProperties-2.8.0
classNameAdditional css classstring | string[]--
styleAdditional styleCSSProperties--
onChangeCallback fired when Affix state is changed(affixed: boolean) => void--
targetSpecifies the scrollable area DOM Element() => HTMLElement | null | Window() => window-
targetContainerOuter scrollable DOM element of target. Affix will listen to the container's scroll event and update the its position correspondingly.It's to solve the problem that Affix may escape the container when the container is not window.() => HTMLElement | null | Window--