组件 / 导航 / cu-sticky 吸顶容器

cu-sticky

吸顶容器用于包裹滚动时需要停在顶部的内容块,常见于分组标题与筛选区。

已知缺口
01 / Install

引入组件

整目录放进工程即用:easycom 按标签逐个解析,页面写哪个组件就只编译哪个,不写 import,也没有全局注册这一步;单个组件也可以显式引入(第 4 步,可选);主题层在 App.uvue 引入一次即可。

install
# 1  整目录放进工程(easycom 自动解析)
your-project/
└── uni_modules/
    └── colorui-x/

# 2  App.uvue 全局引入主题层(一次)
@import 'uni_modules/colorui-x/theme/base.css';
@import 'uni_modules/colorui-x/theme/layout.css';
@import 'uni_modules/colorui-x/theme/color.scss';  // 颜色工具类(.bg-* / .text-*)

# 3  页面里直接用,不写 import(只编译用到的组件)
<cu-sticky />

# 4  单个组件也可以显式引入(可选,不用 easycom 时)
import CuSticky from '@/uni_modules/colorui-x/components/cu-sticky/cu-sticky.uvue'

组件样式自包含(只依赖包内 token.scss),主题层是给页面工具类用的,不参与组件注册。两种安装方式见 快速上手。

02 / Platform

平台支持

AndroidiOSHarmonyOSWeb(H5)微信小程序支付宝小程序
√ — — √ — —

Android 列取自本仓真机逐屏判定(√ 已验 / — 待复核 / × 已登记缺陷,本件当前为「真机已验」)。

Web 列只表示已编出可运行的预览产物,非真机渲染。

其余三端没有任何实测证据,不作支持承诺。

03 / Demo

代码演示

01

完整用法

App 端 sticky 不生效,退为静态容器

uvue
<cu-sticky>
  <text>
    默认档:bgColor=transparent → 应无底色,只有文字
  </text>
</cu-sticky>
<cu-sticky bgColor="#39b54a">
  <text>
    实色底:#39b54a 经内联生效(prop 色不走 token)
  </text>
</cu-sticky>
<cu-sticky bgColor="">
  <text>
    bgColor 传空串 → 拼出 background-color:; 应回落无底色且不报错
  </text>
</cu-sticky>
<cu-sticky :disabled="true" bgColor="#f37b1d">
  <text>
    disabled=true → position:static(参考实现 :64),底色仍应生效
  </text>
</cu-sticky>
<cu-sticky>
  <text>
    S1 默认档:top 0,无底色 → 应第一个粘在容器顶
  </text>
</cu-sticky>
<cu-sticky :offsetTop="50">
  <text>
    S2 offsetTop=50 → top:100rpx
  </text>
</cu-sticky>
<cu-sticky :offsetTop="100">
  <text>
    S3 offsetTop=100 → top:200rpx
  </text>
</cu-sticky>
<cu-sticky :customNavHeight="60" :offsetTop="40" zIndex="9999">
  <text>
    S4 customNavHeight 60 + offsetTop 40 → 同为 top:200rpx,且 zIndex=9999 → 应与 S3 重合并盖住它(S4 可见)
  </text>
</cu-sticky>
<cu-sticky :offsetTop="150" zIndex="1" bgColor="#e54d42">
  <text>
    S5 offsetTop=150 → 300rpx,zIndex='1'(比默认的 970 低)
  </text>
</cu-sticky>
<cu-sticky :offsetTop="150" bgColor="#0081ff">
  <text>
    S6 同 top 300rpx、zIndex 留空 → 970,应盖住 S5
  </text>
</cu-sticky>
<cu-sticky :disabled="true" bgColor="#6739b6">
  <text>
    S7 disabled=true → static,应随流滚走、绝不粘顶
  </text>
</cu-sticky>
<cu-sticky bgColor="">
  <text>
    S8 bgColor 空串(top 0)→ 应无底色但仍吸附
  </text>
</cu-sticky>
<cu-sticky index="X">
  <text>
    S9 传 index='X'(声明未消费)→ 应与 S1 同形(top 0、无底色)
  </text>
</cu-sticky>
<cu-sticky>
  <!-- 空槽:本格不给任何内容,用来验 sticky 是否能零高且不报错 -->
</cu-sticky>
<cu-sticky :customStyle="boxStyle" customClass="custom-class" :offsetTop="200">
  <text>
    S10 customStyle(左右内距 20rpx)+ customClass(页面侧圆角)→ top:400rpx
  </text>
</cu-sticky>
<cu-sticky :offsetTop="250" bgColor="#f1f1f1">
  <text>
    S11 超长文案:这是一段用来观察吸顶容器是否会限制内容换行的超长文案,超长文案继续重复直到撑满整行并触发换行,本仓未给本件任何宽高与截断声明(参考实现同形),文字应正常换行而容器随内容长高。
  </text>
</cu-sticky>
<cu-sticky :offsetTop="0">
  <text>
    S12 嵌套:外层 sticky(top 0),内层再套一个 sticky
  </text>
  <cu-sticky :offsetTop="60" bgColor="#cce6ff">
    <text>
      S12-in 内层 sticky top:120rpx(App 端嵌套 sticky 的包含块语义未实测 → 只登记现象)
    </text>
  </cu-sticky>
</cu-sticky>
<cu-sticky :offsetTop="400">
  <text>
    S13 offsetTop=400 → 800rpx:本行用来确认 top 超过视口一半时的表现,且它应比 S1~S11 都低
  </text>
</cu-sticky>
02

读法

这一节的交互由演示页脚本驱动,效果见右侧实时预览。

03

各档速查

这一节的交互由演示页脚本驱动,效果见右侧实时预览。

04

取值与事件

这一节的交互由演示页脚本驱动,效果见右侧实时预览。

04 / Props · Events · Slots

全量 API

props

参数说明类型默认值
吸顶偏移
offsetTop吸顶时距顶部的偏移,px 契约值;运行时 ×2 换算成 rpxNumber0
customNavHeight额外的导航栏高度,px 契约值;与 offsetTop 相加作为最终偏移Number0
disabled关闭吸顶,容器按普通文档流滚动Booleanfalse
bgColor容器底色,默认透明String'transparent'
zIndex吸顶时的层级,传空串时用内置档位String''
交互与样式
customStyle根节点内联样式Object(): UTSJSONObject => ({})
customClass追加到根节点的类名String''
声明保留但不被消费
index供外部自用的标识位;本仓未读取,传参不生效String''
url不产生跳转,传参不生效String''
linkType不产生跳转,同上String'navigateTo'

events

本件不派发事件。

slots

slot说明
(默认)吸顶时保持可见的内容;不填时容器为零高节点
05 / Notes

注意事项

19 · 吸顶不生效 :粘性定位在 App 端被静默丢弃,本件只能当普通随流容器用,滚到页末也不会粘住;要吸顶须在页面侧自绘固定层并补一块占位盒。

无占位与事件 :组件不生成占位块,也不派发任何事件,外部无法感知它是否吸顶;自绘吸顶时的高度占位与状态判断都要放回页面维护。