Skip to content

Sticky 粘性布局

介绍

当组件滚出屏幕范围时,固定在指定位置。

基础用法

将内容包裹在组件内,即可实现粘性布局。

html
<vd-sticky>
  <vd-button type="primary">基础用法</vd-button>
</vd-sticky>

吸顶距离

通过 top 属性来设置吸顶距离。

html
<vd-sticky :top="100">
  <vd-button type="primary">吸顶距离</vd-button>
</vd-sticky>

吸底距离

position 设置为 bottom 可以让组件吸附在底部。

html
<vd-sticky :bottom="100" position="bottom">
  <vd-button type="warning">吸底距离</vd-button>
</vd-sticky>

同时设置吸顶距离和吸底距离

通过 topbottom 属性同时设置吸顶距离和吸底距离。

html
<vd-sticky :top="200" :bottom="50" :position="['top', 'bottom']">
  <vd-button type="primary">吸顶(200)、吸底(50)</vd-button>
</vd-sticky>

相对容器

通过 sticky-box 组件来设置相对容器。

html
<vd-sticky-box custom-class="sticky-container">
  <vd-sticky>
    <vd-button type="primary">相对容器</vd-button>
  </vd-sticky>
</vd-sticky-box>
ts
import { randomId } from '@vital-design/shared';

const containerId = randomId();
scss
.sticky-container {
  height: 200px;
  background: $vd-background;
  padding-left: 50%;
}

API

Props

参数说明类型默认值
position吸附位置,可选值 top bottomstickyPosition | stickyPosition[]top
top吸附时,与顶部偏移距离string | number | (string | number)[]0
bottom吸附时,与底部偏移距离string | number | (string | number)[]0
offset-navbar偏移距离是否添加导航栏高度,页面有 Navbar 组件时会添加对应高度booleantrue
offset-tabbar偏移距离是否添加标签栏高度,页面有 Tabbar 组件时会添加对应高度booleantrue
container相对容器的节点匹配选择器string-
z-index层级string | number99

代替原始 Props

参数说明类型默认值
custom-id根节点idstring-
custom-class根节点类名ClassValue-
custom-style根节点样式StyleValue-

Events

事件名说明回调参数
click点击时触发MouseEvent
resize尺寸改变时触发NodeRect
change吸顶/底改变时触发stickyOnChangeEvent

Slots

名称说明
default自定义内容

Expose

方法名说明参数返回值
update手动更新吸附状态计算--

赣ICP备2025061025号-1