Skip to content

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

AttributeDescriptionTypeDefault
positionOffset position, can be set to bottomstringtop
offset-topOffset top, supports px``vw``vh``rem unit, default px*numberstring*
offset-bottomOffset bottom, supports px``vw``vh``rem unit, default px*numberstring*
z-indexz-index when sticky*numberstring*
containerContainer DOMElement-

Events

EventDescriptionArguments
changeEmitted when sticky status changedisFixed: boolean
scrollEmitted 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.

NameDefault ValueDescription
--van-sticky-z-index99-

Enterprise-level mobile solution based on Vant