JGuang

走马灯

Swiper · Ant Design Mobile

交互预览
npm install antd-mobile@5.43.0

走马灯 Swiper

一组轮播的区域。

安装

npm install antd-mobile@5.43.0

antd-mobile 是移动网页组件库,不是 React Native 或小程序组件。React 19 项目需在入口用 unstableSetRender 注册 createRoot,否则 Toast、Dialog 等命令式 API 无法渲染;深色模式通过 <html data-prefers-color-scheme="dark"> 切换。

示例来自 Ant Design Mobile 官方仓库(MIT),固定在 npm 5.43.0 对应的提交;示例中的 DemoBlock 等展示辅助组件由 JGuang 重写,不属于 antd-mobile 的 API。

一组轮播的区域。

何时使用

  • 当有一组平级的内容。
  • 当内容空间不足时,可以用走马灯的形式进行收纳,进行轮播展现。
  • 常用于一组图片或卡片轮播。

示例

基础用法

不是满宽的滑块

指示器

全屏引导

竖向

当你使用竖向的 Swiper 时,请务必通过 --height CSS 变量设置一下它的高度,否则 Swiper 的会显示异常。

虚拟滚动

通过 total 与 renderProps 渲染,不支持 loop

Swiper

属性

属性说明类型默认值版本
allowTouchMove是否允许手势滑动booleantrue
autoplay是否自动切换booleanfalsereverse
autoplayInterval自动切换的间隔,单位为 msnumber3000
defaultIndex初始位置number0
direction方向,默认是水平方向'horizontal' | 'vertical''horizontal'
indicator自定义指示器(total: number, current: number) => ReactNode-
indicatorProps指示器的相关属性支持 PageIndicator 的 color style className 属性-
loop是否循环booleanfalse
onIndexChange切换时触发(index: number, info: { source: SwiperIndexChangeSource }) => void-
rubberband是否在拖动超出内容区域时启用橡皮筋效果,仅在非 loop 模式下生效booleantrue
slideSize滑块的宽度百分比number100
stuckAtBoundary是否在边界两边卡住,避免出现空白,仅在非 loop 模式且 slideSize < 100 时生效booleantrue
trackOffset滑块轨道整体的偏移量百分比number0
total虚拟滚动时渲染总数,需要配合 renderProps 一同使用。(不支持 loop)number5.34.0
stopPropagation阻止某些事件的冒泡PropagationEvent[][]5.28.0
type PropagationEvent = 'mouseup' | 'mousemove' | 'mousedown'
type SwiperIndexChangeSource = 'auto' | 'swipe' | 'resize'

CSS 变量

属性说明默认值
--border-radius整体组件的圆角0
--height高度auto
--track-padding轨道区域的 padding0
--width宽度100%

Ref

属性说明类型
swipeNext切换到下一张() => void
swipePrev切换到上一张() => void
swipeTo切换到指定索引(index: number) => void

Swiper.Item

属性

属性说明类型默认值
onClick点击滑块时触发(e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void-