Sticky 吸顶
基于 CSS sticky 的顶/底吸附;只有监听 change 时才创建交叉观察器。
引入
uni-app 配置 easycom 后使用 hui-sticky。普通 Vue 从 @nscode/handy-uni/web 具名导入 HuiSticky,或安装预览插件。
示例
交互与事件结果
操作组件后,下方显示最近一次事件及完整参数。弹层选择器先修改草稿,确认后提交;取消保留原值。
vue
<script setup lang="ts">
import { ref } from 'vue'
const result = ref('')
function event0(arg0: unknown) {
result.value = 'change' + ': ' + JSON.stringify([arg0])
}
</script>
<template>
<view>
<hui-sticky :offset-top="12" @change="event0"
><text>吸附内容 / Sticky content</text></hui-sticky
>
<text aria-live="polite">{{ result }}</text>
</view>
</template>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
position | "top" | "bottom" | undefined | 'top' | 吸附边缘 top 或 bottom。 |
offset-top | number | undefined | 0 | 吸顶偏移 px,自定义导航栏时传其高度。 |
offset-bottom | number | undefined | 0 | 吸底偏移 px。 |
safe-area-inset-bottom | boolean | undefined | false | 底部预留安全区。 |
z-index | number | undefined | — | 显式层号;未传时由页面弹层栈分配。 |
disabled | boolean | undefined | false | 禁用用户操作;优先于表单继承配置。 |
custom-class | string | undefined | '' | 根节点附加类名。 |
custom-style | string | CSSProperties | undefined | — | 根节点附加样式。 |
Events
| 事件 | 参数 | 说明 |
|---|---|---|
change | (isStuck: boolean) | 提交后的值与上下文,发生在模型更新事件之后。 |
Slots
| 插槽 | 说明 |
|---|---|
default | 默认内容。 |
方法
| 方法 | 签名 |
|---|
类型
StickyProps、StickyEmits。公开类型由包入口统一导出。
平台说明
| 范围 | 行为与验证边界 |
|---|---|
| 公共实现 | 粘性位置经平台测量维护,页面滚动由页面按文档转发,组件不导入页面生命周期。 |
| 设备待验 | 核对导航栏偏移、容器边界、页面切换和内容高度变化;实际设备检查滚动时定位。 |
对应操作见 examples/uni-demo/smoke/Sticky.md。单元测试不替代真机验收。