Sticky - Vant 4
Sticky
Intro
The sticky component is consistent with the effect achieved by the position: sticky property in CSS, in that when the component is within screen range, it will follow the normal layout arrangement, and when the component rolls out of screen range, it will always be fixed at the top of the screen.
Install
Register component globally via app.use, refer to Component Registration for more registration ways.
js
import { createApp } from'vue'; import { Sticky } from'vant'; const app = createApp(); app.use(Sticky);Usage
Basic Usage
html
Offset Top
html
Set Container
html
js
exportdefault { setup() { const container = ref(null); return { container }; }, };Offset Bottom
html
API
Props
| Attribute | Description | Type | Default |
|---|---|---|---|
| position | Offset position, can be set to bottom | string | top |
| offset-top | Offset top, supports px``vw``vh``rem unit, default px | *number | string* |
| offset-bottom | Offset bottom, supports px``vw``vh``rem unit, default px | *number | string* |
| z-index | z-index when sticky | *number | string* |
| container | Container DOM | Element | - |
Events
| Event | Description | Arguments |
|---|---|---|
| change | Emitted when sticky status changed | isFixed: boolean |
| scroll | Emitted when scrolling | { scrollTop: number, isFixed: boolean } |
Types
The component exports the following type definitions:
ts
importtype { StickyProps, StickyPosition } from'vant';Theming
CSS Variables
The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.
| Name | Default Value | Description |
|---|---|---|
| --van-sticky-z-index | 99 | - |