Skip to content

Affix

A high-performance affix component that fixes content to the top or bottom of the viewport or a target container while preserving layout space.

Performance Architecture

YhAffix employs a dual-layer observation system:

  • IntersectionObserver: Monitors distance from the trigger zone. When the element is far away from the viewport, high-frequency recalculation is skipped to reduce overhead.
  • ResizeObserver: Observes the placeholder, content, and target container so size changes can update the fixed position without visible jitter.

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

YhAffix supports themeOverrides. The component stylesheet directly consumes the following affix-specific CSS variables:

VariableDefaultDescription
--yh-affix-z-indexvar(--yh-z-index-fixed)Z-index token used in the fixed state
--yh-affix-shadowvar(--yh-shadow-md)Shadow applied while affixed
--yh-affix-transition-durationvar(--yh-duration-base)Transition duration for fixed-state updates
--yh-affix-transition-timingcubic-bezier(0.4, 0, 0.2, 1)Transition timing function

Type Exports

NameDescription
YhAffixPropsProps type for YhAffix
YhAffixEmitsEmits type for YhAffix
YhAffixSlotsSlots type for YhAffix
YhAffixExposeExpose type for YhAffix
YhAffixInstancePublic instance type for YhAffix

Released under the MIT License.