外观
Sticky 粘性定位
约 300 字大约 1 分钟
2025-10-31
基本使用
由于粘性定位需要实时监听滚动条的位置,从而判断是否需要粘性定位,由于组件无法得知页面的滚动条信息,只能在页面的onPageScroll生命周期 中获得滚动条的位置,故需要在页面监听onPageScroll生命周期,实时获得滚动条的位置,通过Props传递给组件。
<template>
<view class="container">
<un-sticky :scrollTop="scrollTop">
<un-button text="吸顶按钮" type="success"></un-button>
</un-sticky>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue';
const scrollTop = ref(0);
onPageScroll((event: OnPageScrollOptions) => {
scrollTop.value = event.scrollTop;
});
</script>吸顶距离
通过offsetTop参数设置组件在吸顶时与顶部的距离
<un-sticky offset-top="200">
<text>塞下秋来风景异,衡阳雁去无留意</text>
</un-sticky>API
Props
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| scrollTop | 滚动距离,用于判断是否需要吸顶 | Number | 0 |
| offsetTop | 吸顶时与顶部的距离 | String | Number | 0 |
| customNavHeight | 导航栏高度,自定义导航栏时,需要传入此值 | String | Number | 0 |
| disabled | 是否禁用吸顶功能 | Boolean | false |
| bgColor | 组件背景颜色 | String | #ffffff |
| zIndex | 吸顶时的z-index值 | String | Number | - |
