Affix
Keep content affixed to the top or bottom of a scrollable area as the page scrolls.
Import
import { HuiAffix } from '@nscode/handy-ui'
Basic Usage
Content becomes affixed when it reaches offset pixels from the top, then remains at that distance. A placeholder of the same height preserves its original space so following content does not jump.
Without listen-to, the component listens to page scrolling. Examples on this page use a fixed-height scrollable container referenced by listen-to; see Custom Scroll Containers below. Scroll down within the box:
<script setup lang="ts">
import { HuiAffix, HuiButton } from '@nscode/handy-ui'
</script>
<template>
<div id="affix-demo-basic" class="box">
<p class="line">Scroll down</p>
<HuiAffix listen-to="#affix-demo-basic" :offset="20">
<HuiButton type="primary">Affix 20px from the top</HuiButton>
</HuiAffix>
<p v-for="i in 20" :key="i" class="line">Row {{ i }} content</p>
</div>
</template>
<style scoped>
.box {
height: 240px;
overflow: auto;
padding: 0 16px;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.line {
margin: 12px 0;
color: var(--hui-text-color-secondary);
}
</style>
Affixing to the Top or Bottom
position accepts top (the default) or bottom. With position="bottom", content is affixed to the bottom when it is less than offset pixels from the scrollable area's bottom, including when it is still below the visible area. As you scroll down and its distance from the bottom exceeds offset, it returns to its original position.
The left example uses top; the right uses bottom. The right button's original position is in the middle of the content, initially below the viewport. It therefore starts affixed at the bottom and returns to its original position when you scroll to it.
<template>
<HuiSpace wrap align="start">
<div id="affix-demo-top" class="box">
<p class="line">position="top"</p>
<HuiAffix listen-to="#affix-demo-top" position="top" :offset="10">
<HuiButton type="primary">Affix to the top (top)</HuiButton>
</HuiAffix>
<p v-for="i in 20" :key="i" class="line">Row {{ i }} content</p>
</div>
<div id="affix-demo-bottom" class="box">
<p class="line">position="bottom"</p>
<p v-for="i in 10" :key="i" class="line">Row {{ i }} content</p>
<HuiAffix listen-to="#affix-demo-bottom" position="bottom" :offset="10">
<HuiButton type="success">Affix to the bottom (bottom)</HuiButton>
</HuiAffix>
<p v-for="i in 10" :key="i + 10" class="line">Row {{ i + 10 }} content</p>
</div>
</HuiSpace>
</template>
<style scoped>
.box {
width: 260px;
height: 240px;
overflow: auto;
padding: 0 16px;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.line {
margin: 12px 0;
color: var(--hui-text-color-secondary);
}
</style>
Separate Trigger and Affixed Offsets
trigger-top controls when affixing starts, while top controls the distance from the top once affixed. They can differ. trigger-bottom and bottom provide the corresponding bottom behavior.
When only one is provided, the other uses that value. When neither is provided, both use offset. Resolution order:
- Trigger distance:
triggerTop→top→offset(bottom:triggerBottom→bottom→offset). - Affixed position:
top→triggerTop→offset(bottom:bottom→triggerBottom→offset).
The left example starts affixing at 60px from the top, then moves to an affixed offset of 10px, producing an upward jump. The right example shows the corresponding bottom configuration.
<template>
<HuiSpace wrap align="start">
<div id="affix-demo-trigger-top" class="box">
<p v-for="i in 3" :key="i" class="line">Row {{ i }} content</p>
<HuiAffix listen-to="#affix-demo-trigger-top" :trigger-top="60" :top="10">
<div class="bar">Trigger at 60px from the top; affix at 10px</div>
</HuiAffix>
<p v-for="i in 20" :key="i + 3" class="line">Row {{ i + 3 }} content</p>
</div>
<div id="affix-demo-trigger-bottom" class="box">
<p v-for="i in 10" :key="i" class="line">Row {{ i }} content</p>
<HuiAffix
listen-to="#affix-demo-trigger-bottom"
position="bottom"
:trigger-bottom="60"
:bottom="10"
>
<div class="bar">Trigger within 60px of the bottom; affix at 10px</div>
</HuiAffix>
<p v-for="i in 10" :key="i + 10" class="line">Row {{ i + 10 }} content</p>
</div>
</HuiSpace>
</template>
<style scoped>
.box {
width: 260px;
height: 240px;
overflow: auto;
padding: 0 16px;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.line {
margin: 12px 0;
color: var(--hui-text-color-secondary);
}
.bar {
padding: 8px 12px;
background-color: var(--hui-color-primary);
color: var(--hui-color-white);
border-radius: var(--hui-border-radius);
}
</style>
Tracking Affixed State
change emits the current affixed state whenever it changes. scroll emits on each position recalculation, including mounting, scrolling, window resizing, and content-height changes, with scrollTop and fixed.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiAffix } from '@nscode/handy-ui'
const fixed = ref(false)
const scrollTop = ref(0)
const changeCount = ref(0)
const handleChange = (value: boolean) => {
fixed.value = value
changeCount.value += 1
}
const handleScroll = (params: { scrollTop: number; fixed: boolean }) => {
scrollTop.value = params.scrollTop
}
</script>
<template>
<div id="affix-demo-change" class="box">
<p class="line">Scroll down</p>
<HuiAffix
listen-to="#affix-demo-change"
:offset="0"
@change="handleChange"
@scroll="handleScroll"
>
<div
:style="{
padding: '12px',
backgroundColor: fixed ? 'var(--hui-color-primary)' : 'var(--hui-bg-color)',
color: fixed ? 'var(--hui-color-white)' : 'var(--hui-text-color)',
border: '1px solid var(--hui-border-color)'
}"
>
{{ fixed ? 'Affixed' : 'Not affixed' }}
</div>
</HuiAffix>
<p v-for="i in 20" :key="i" class="line">Row {{ i }} content</p>
</div>
<p style="margin-top: 12px">
change emitted {{ changeCount }} times; currently {{ fixed ? 'Affixed' : 'Not affixed' }};
scrollTop:{{ scrollTop }}
</p>
</template>
<style scoped>
.box {
height: 240px;
overflow: auto;
padding: 0 16px;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.line {
margin: 12px 0;
color: var(--hui-text-color-secondary);
}
</style>
Custom Scroll Containers
For content inside a scrollable element, set listen-to to a CSS selector, the element, or a function returning it. Affixed positioning is relative to that container: offset pixels below its top, following the container if it moves on the page. target serves the same purpose and is used only when listen-to is omitted.
<script setup lang="ts">
import { HuiAffix } from '@nscode/handy-ui'
</script>
<template>
<div
id="affix-demo-box"
style="height: 300px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<div style="height: 80px">Content above</div>
<HuiAffix listen-to="#affix-demo-box" :offset="10">
<div
style="padding: 12px; background-color: var(--hui-color-primary); color: var(--hui-color-white)"
>
Affix at the container's top
</div>
</HuiAffix>
<div style="height: 800px">Content below</div>
</div>
</template>
When obtaining the container through a ref, pass a function:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiAffix } from '@nscode/handy-ui'
const boxRef = ref<HTMLElement>()
// Use a stable function rather than an inline template arrow; a new function on every render causes listeners to be rebound
const getBox = () => boxRef.value!
</script>
<template>
<div
ref="boxRef"
style="height: 300px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<div style="height: 80px">Content above</div>
<HuiAffix :listen-to="getBox" :offset="10">
<div
style="padding: 12px; background-color: var(--hui-color-warning); color: var(--hui-color-white)"
>
Affixed content (listen-to function)
</div>
</HuiAffix>
<div style="height: 800px">Content</div>
</div>
</template>
A selector passed to target works like listen-to:
<template>
<div
id="affix-demo-target"
style="height: 300px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<div style="height: 80px">Content above</div>
<HuiAffix target="#affix-demo-target" :offset="10">
<div
style="padding: 12px; background-color: var(--hui-color-info); color: var(--hui-color-white)"
>
Container specified with target
</div>
</HuiAffix>
<div style="height: 800px">Content below</div>
</div>
</template>
Absolute Positioning
css-position accepts fixed (the default) or absolute. The examples above use the default position: fixed when affixed. With css-position="absolute", top and bottom are relative to the nearest positioned ancestor. Use a position: relative outer wrapper containing the scrollable element, without setting position on the scrollable element itself. Place Affix inside that element and point listen-to at it. The affixed content is then positioned relative to the outer wrapper and does not scroll away with the content.
<script setup lang="ts">
import { HuiAffix } from '@nscode/handy-ui'
</script>
<template>
<div style="position: relative">
<div
id="affix-absolute-box"
style="height: 300px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<div style="height: 80px">Content above</div>
<HuiAffix css-position="absolute" listen-to="#affix-absolute-box" :top="10" :trigger-top="10">
<div
style="padding: 12px; background-color: var(--hui-color-success); color: var(--hui-color-white)"
>
Absolutely positioned Affix
</div>
</HuiAffix>
<div style="height: 800px">Content below</div>
</div>
</div>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| position | 'top' | 'bottom' | 'top' | Whether to affix to the top or bottom |
| offset | number | 0 | Default trigger and affixed offsets in pixels, used when top, bottom, triggerTop, and triggerBottom are omitted |
| listenTo | string | HTMLElement | Document | Window | (() => HTMLElement | Document | Window) | - | Scroll container. Strings are CSS selectors; functions are called on mount. Takes precedence over target. Document is treated as window |
| target | string | HTMLElement | Window | - | Scroll container, used like listenTo when listenTo is omitted. If both are omitted, listens to page scrolling (window) |
| zIndex | number | 100 | Stacking level while affixed |
| cssPosition | 'fixed' | 'absolute' | 'fixed' | CSS positioning while affixed; see the absolute example above |
| top | number | - | Affixed distance from the top in pixels. Falls back to triggerTop, then offset |
| bottom | number | - | Affixed distance from the bottom in pixels. Falls back to triggerBottom, then offset |
| triggerTop | number | - | Distance from the top that triggers affixing, in pixels. Falls back to top, then offset |
| triggerBottom | number | - | Distance from the bottom that triggers affixing, in pixels. Falls back to bottom, then offset |
Events
| Event | Callback arguments | Description |
|---|---|---|
| change | (fixed: boolean) | Emitted when the affixed state changes |
| scroll | (params: { scrollTop: number; fixed: boolean }) | Emitted whenever position is recalculated: mounting, container or page scrolling, window resizing, or content-height changes |
Slots
| Slot | Scope arguments | Description |
|---|---|---|
| default | - | Content to affix |
Types
import type {
AffixProps,
AffixEmits,
AffixPosition,
AffixCssPosition,
AffixListenTo,
AffixTarget
} from '@nscode/handy-ui'
type AffixPosition = 'top' | 'bottom'
type AffixCssPosition = 'fixed' | 'absolute'
type AffixListenTo =
| string
| HTMLElement
| Document
| Window
| (() => HTMLElement | Document | Window)
type AffixTarget = string | HTMLElement | Window
interface AffixProps {
position?: AffixPosition
offset?: number
target?: AffixTarget
zIndex?: number
cssPosition?: AffixCssPosition
listenTo?: AffixListenTo
top?: number
bottom?: number
triggerTop?: number
triggerBottom?: number
}
interface AffixEmits {
(e: 'change', fixed: boolean): void
(e: 'scroll', params: { scrollTop: number; fixed: boolean }): void
}