<div class="icen-carousel"> <div class="icen-carousel-track"> <div class="icen-carousel-slide"><div style="height:180px;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:6px;background:linear-gradient(135deg,var(--icen-token-accent),var(--icen-token-accent-deep));color:var(--icen-token-on-accent)"><strong>随手的组件</strong><span style="font-size:12px;opacity:.85">一行引入,行为自动接上</span></div></div> <div class="icen-carousel-slide"><div style="height:180px;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:6px;background:linear-gradient(135deg,var(--icen-token-success),var(--icen-token-info));color:#fff"><strong>6 套主题</strong><span style="font-size:12px;opacity:.85">同一套令牌,6 种气质</span></div></div> <div class="icen-carousel-slide"><div style="height:180px;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:6px;background:linear-gradient(135deg,var(--icen-token-accent-cold),var(--icen-token-accent));color:#fff"><strong>键盘与触屏</strong><span style="font-size:12px;opacity:.85">方向键、滑动、圆点直达</span></div></div> </div> <button class="icen-carousel-arrow icen-carousel-arrow--prev" type="button" aria-label="上一张"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width="16" height="16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" focusable="false" class="icen-symbol" aria-hidden="true"><path d="M12.25 5.5 7.75 10l4.5 4.5"></path></svg></button> <button class="icen-carousel-arrow icen-carousel-arrow--next" type="button" aria-label="下一张"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width="16" height="16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" focusable="false" class="icen-symbol" aria-hidden="true"><path d="M7.75 5.5 12.25 10l-4.5 4.5"></path></svg></button> <span class="icen-carousel-index" aria-hidden="true"></span> <div class="icen-carousel-dots"></div></div>
特性
- 通道:箭头 / 圆点 / 触摸滑动 / 键盘,首尾循环
- 覆盖件:圆点与箭头用玻璃语言,深浅媒质上都可辨;触屏命中区 ≥ 44px
- RTL:方向键按视觉方向镜像
- 读屏:隐藏状态行通报「第 i / n 张」;标记里放
.icen-carousel-index槽位即同步索引
接口
- 初始化
initCarousel()(kit 一行入口可直接命名导入)- 导出
-
initCarousel - 事件
-
icen:carousel-changedetail 契约见IcenEventMap - 类型
dist/behaviors/carousel.d.mts(随 npm 包发布,含全部 JSDoc 约束)
安装
bun add @icen.ai/ui pnpm add @icen.ai/ui npm install @icen.ai/ui yarn add @icen.ai/ui 一行引入该组件(CSS 与 behavior 自动带上,无需关心内部 CSS 文件名):
import { initCarousel } from '@icen.ai/ui/kit/carousel';initCarousel();
也可以用脚手架直接打印引入行:
bunx --bun @icen.ai/ui add carousel
Astro 项目注意:kit 入口里的 CSS import 在页面 <script> 里会被 Astro 的 client bundle 摇掉——请改在布局 frontmatter 里引 CSS(import '@icen.ai/ui/components/media.css';),JS 行为仍可走 kit。Vite SPA / webpack 项目无此问题。
用法
import { initCarousel } from '@icen.ai/ui/behaviors/carousel';<div class="icen-carousel"> <div class="icen-carousel-track"><div class="icen-carousel-slide">…</div>×n</div> <button class="icen-carousel-arrow icen-carousel-arrow--prev" aria-label="上一张">…</button> <button class="icen-carousel-arrow icen-carousel-arrow--next" aria-label="下一张">…</button> <span class="icen-carousel-index" aria-hidden="true"></span> <!-- 可选:自动同步「i / n」 --> <div class="icen-carousel-dots"></div> <!-- 留空,圆点自动生成 --></div>const dispose = initCarousel(); // 幂等;宿主卸载时 dispose()// 键盘 ←/→/Home/End(RTL 镜像);触屏自动开滑动;读屏通报「第 i / n 张」// 事件:icen:carousel-change { index, count }(仅用户导航时派发)