Skip to content

Affix (Next-Gen)

A high-performance affix component based on physics engine algorithms and native browser observers. Surpasses industry standards in both functionality and performance.

Performance Architecture (Surpassing Engine)

YhAffix employs a dual-layer observation system:

  • IntersectionObserver: The engine automatically monitors the distance between the element and the trigger zone. When the element is more than 500px away from the viewport trigger point, all high-frequency calculations are automatically cut off, achieving extreme power efficiency.
  • ResizeObserver: Deeply observes size changes across three parties — the placeholder, the content, and the target container — ensuring zero jitter during async content loading.

Basic Usage

Set the offset and enjoy instant snap-to feedback.

Basic Usage

Smart Bottom Affix

In position="bottom" mode, the component automatically detects the viewport bottom.

Scroll to see the 100px offset bottom affix

Bottom Affix

Teleport Positioning

Core Breakthrough: For the issue where traditional fixed positioning fails inside parent containers with transform, filter, or perspective properties, YhAffix supports the teleported prop. It teleports the rendered node to body and maintains perfect visual alignment through coordinate projection algorithms.

This container has a CSS transform (scale: 0.9). Normal affix would misalign, but this component handles it perfectly via Teleport.

Teleport

Scoped Slot

Supports real-time access to the fixed state via scoped slots, enabling more complex UI interaction logic.

Scoped Slot

Dynamic Disable

Use the disabled prop to toggle the state, suitable for permission control or complex dynamic interaction scenarios.

Dynamic Disable

Use in Nuxt

YhAffix fully supports Nuxt 3/4 SSR rendering. Since affixing depends on browser window coordinates, the component has already handled client-side activation logic internally, so you can use it with confidence.

Best Practice: In Nuxt projects, pages are usually nested within NuxtLayout or various levels of complex animation components. It is recommended to enable the teleported prop, which ensures the affix element is hoisted to the root node in the DOM tree, avoiding any intermediate-level style interference.

In Nuxt environments, enabling teleported ensures the most robust positioning behavior.

Nuxt SSR Support

API

Props

PropDescriptionTypeDefault
offsetOffset distance to trigger fixingnumber0
positionAffix position'top' | 'bottom''top'
targetTarget container selector. Affixing stops outside this containerstring
z-indexZ-index when fixednumber100
teleportedWhether to teleport to a specified node for rendering, solving parent transform positioning issuesbooleanfalse
append-toTarget container node for teleport, used with teleportedstring | HTMLElement'body'
disabledWhether to disable the componentbooleanfalse
affix-styleExtra styles passed to the fixed containerCSSProperties{}

Events

Event NameDescriptionParameters
changeTriggered when the fixed state changes(fixed: boolean)
scrollTriggered in real-time while scrolling(payload: { scrollTop: number, fixed: boolean })

Slots

Slot NameDescriptionScope Parameters
defaultContent slot to be fixed{ fixed: boolean }

Expose

NameDescriptionType
updateManually trigger position calculation and state update() => void
fixedWhether currently in the fixed stateRef<boolean>
scrollTopCurrent vertical scroll distance of the scroll containerRef<number>

Theme Variables (CSS Variables)

All color variables are integrated with the global theme system, automatically supporting dark mode:

VariableDefaultDescription
--yh-affix-z-index100Default z-index in fixed state
--yh-affix-bg-colorvar(--yh-bg-color-overlay)Affix content background color
--yh-affix-shadowvar(--yh-shadow-md)Shadow effect in fixed state
--yh-affix-transition-durationvar(--yh-transition-duration)Animation duration when entering fixed state
--yh-affix-transition-timingvar(--yh-transition-timing)Animation easing curve

Released under the MIT License.