Carousel 轮播
触摸拖拽(位移 / 速度双阈值)、自动轮播(hover 与拖拽期间暂停)、循环回绕与边界钳制、 dots 指示器与箭头。实例方法 prev / next / goTo 以句柄形态暴露。
契约源:
lh-ui-core/specs/carousel.json· 黄金用例:lh-ui-core/cases/carousel.json(10 条,已翻译为 hypium 测试)
端侧落点说明
spec behaviors 明确「移动端可整体封装原生 swiper」——翻页承载于 ArkUI 原生 Swiper(value → index、loop → loop、手势动画 / 回绕交由原生), autoplay 由组件自排 setTimeout 承载以精确控制「拖拽与 hover 期间暂停」以及 「非 loop 到末页自停」。spec 的 default / item 插槽在端内落点为 itemCount(页数)+ itemBuilder(按 index 渲染对应页):
@Builder
carouselPage(index: number) {
Stack() {
Text(`第 ${index + 1} 页`)
}
.width('100%').height('100%').backgroundColor(this.colors.primary)
}
build() {
LhCarousel({ itemCount: 3, itemBuilder: this.carouselPage, lhHeight: 160 })
}页序数学(normalizePageIndex / nextPageIndex / prevPageIndex / resolveSwipeTarget / stepAutoplay)是跨端纯函数真源(LhCarouselTokens), 拖拽翻页双阈值(位移超页宽 20% 或速度超 0.5px/ms)与之一致。 spec 的 height prop 因 D26 通用属性名避让改名为 lhHeight。
基本用法
itemCount 给页数,itemBuilder 渲染每页内容;默认 loop 回绕 + dots 指示器 (indicatorPosition = bottom 占文档流)。
按钮经 LhCarouselController 调用 prev / next / goTo(黄金用例 next-emits-change)
LhCarousel({ itemCount: 3, itemBuilder: this.carouselPage, lhHeight: 160 })指示器与位置
indicator 取 dots / none;indicatorPosition 取 bottom(文档流,占位在内容之下) 或 overlay(叠加内容上,距底 carousel.indicator.overlayBottom 令牌)。激活圆点放大 carousel.indicator.dotActiveScale(取 color.primary.default,非激活取 color.text.disabled)。
上:overlay 指示器(叠加内容上);下:indicator=none
LhCarousel({
itemCount: 3,
itemBuilder: this.carouselPage,
indicatorPosition: LhCarouselIndicatorPosition.Overlay,
lhHeight: 160,
})自动轮播与循环
autoplay 为间隔 ms(0 或不传 = 不自动);拖拽与 hover 期间暂停。loop = true 时 页序取模回绕(负数也回绕);loop = false 时到边界钳制,自动轮播到末页自停 (黄金用例 autoplay-stops-at-last-nonloop)。
拖拽翻页双阈值:位移超过页宽 20% 或速度超过 0.5px/ms(黄金用例 drag-over-threshold-flips / flick-velocity-flips)
// 自动轮播 + 回绕
LhCarousel({ itemCount: 3, itemBuilder: this.carouselPage, autoplay: 2000 })
// 非 loop:到末页自停(prev/next 到边界不切换)
LhCarousel({ itemCount: 3, itemBuilder: this.carouselPage, autoplay: 1000, loop: false })箭头与方法
showArrow = true 显示左右箭头(桌面形态,点击调用 prev / next); disabled = true 灰化并停用自动轮播与手势(黄金用例 disabled-blocks-gesture)。
disabled 灰化并停用自动轮播与手势
LhCarousel({ itemCount: 3, itemBuilder: this.carouselPage, showArrow: true, controller: this.carouselCtl })
// 句柄方法
this.carouselCtl.prev() // 上一页(loop 时首页回绕到末页)
this.carouselCtl.next() // 下一页
this.carouselCtl.goTo(2) // 跳转到第 3 页(0 基)API
Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
value | 当前页序号(受控;0 基,不传则非受控自走) | number | — |
autoplay | 自动轮播间隔 ms(0 或不传 = 不自动);hover 与拖拽期间暂停 | number | 0 |
loop | 循环回绕;false 时到边界钳制(next/prev 不切换) | boolean | true |
indicator | 指示器形态 | LhCarouselIndicator | Dots |
indicatorPosition | 指示器位置:bottom 文档流 / overlay 叠加内容上 | LhCarouselIndicatorPosition | Bottom |
lhHeight | 高度(vp;spec prop height,D26 通用属性名避让) | number | 200 |
showArrow | 显示左右箭头(桌面端) | boolean | false |
disabled | 禁用:灰化并停用自动轮播与手势 | boolean | false |
itemCount | 页数(spec slots default/item 的端内落点) | number | 0 |
itemBuilder | 每页内容构造器(item 具名插槽的端内落点) | (index: number) => void | — |
ariaLabel | 无障碍名称(未传取页码播报文案) | string | — |
onChange | 页面切换(含非受控自走与受控回写) | (value: number) => void | — |
controller | 宿主句柄(普通属性,不加 @Prop) | LhCarouselController | — |
LhCarouselIndicator
| 值 | 说明 |
|---|---|
Dots | 圆点指示器(默认) |
None | 不显示指示器 |
LhCarouselIndicatorPosition
| 值 | 说明 |
|---|---|
Bottom | 文档流(占位在内容之下) |
Overlay | 叠加内容上(距底 carousel.indicator.overlayBottom) |
LhCarouselController
| 方法 | 说明 |
|---|---|
prev() | 上一页(loop 时首页回绕到末页) |
next() | 下一页(loop 时末页回绕到首页) |
goTo(index) | 跳转到指定页(0 基,越界按 loop 语义规范化) |
令牌对照
| 令牌 | 用途 |
|---|---|
carousel.height.base | 默认高度(200vp) |
carousel.indicator.dotSize | 指示器圆点直径 |
carousel.indicator.dotGap | 指示器圆点间距 |
carousel.indicator.dotActiveScale | 激活圆点放大增量(分数 0.25) |
carousel.indicator.overlayBottom | overlay 形态指示器距底间距 |
color.primary.default | 激活圆点 / 箭头色 |
color.text.disabled | 非激活圆点 / 禁用箭头色 |
color.background.default | 页面底色 |
黄金用例对照
cases/carousel.json 的 10 条断言与 hypium 测试的对应关系(entry/src/test/Carousel.test.ets):
| case id | 断言 | 本地测试 |
|---|---|---|
renders-pages-and-dots | 按内容页数渲染页与指示点 | renders_pages_and_dots |
next-emits-change | next 切换下一页并回调 | next_emits_change |
nonloop-stops-at-last | 非 loop 末页 next 不再切换 | nonloop_stops_at_last |
loop-wraps-negative | loop 模式首页 prev 回绕到末页 | loop_wraps_negative |
drag-over-threshold-flips | 拖拽位移超阈值(页宽 20%)翻页 | drag_over_threshold_flips |
flick-velocity-flips | 快滑(速度超阈值)即使位移不足也翻页 | flick_velocity_flips |
small-drag-keeps-page | 位移不足且速度慢保持当前页 | small_drag_keeps_page |
autoplay-stops-at-last-nonloop | 自动轮播到末页(非 loop)自停 | autoplay_stops_at_last_nonloop |
disabled-blocks-gesture | 禁用态手势不翻页 | disabled_blocks_gesture |
aria-region-announcement | role=region + carousel 播报 + 页码 | aria_region_announcement(无障碍树断言在真机走查) |
另有暗色专项:dark_colors_come_from_dark_tokens / dimensions_are_identical_in_dark_mode,以及 indicator_enums_match_spec / invalid_values_converge。运行:DevEco Studio 右键 entry/src/test → Run Tests。

