Swiper

版本要求:
@ray-js/ray≥0.5.10

描述

幻灯片/轮播容器,支持自动切换、衔接滑动与指示点;使用时需明确整体高度。

属性

属性类型必填默认值说明
dotsboolean否false是否显示面板指示点
dotColorstring否rgba(0,0,0,0.3)指示点颜色
dotActiveColorstring否#000当前选中的指示点颜色
autoplayboolean否false是否自动切换
currentnumber否0当前所在滑块的 index
intervalnumber否5000自动切换时间间隔
durationnumber否500滑动动画时长
circularboolean否false是否采用衔接滑动
verticalboolean否false滑动方向是否为纵向
dataSourceI[]否-列表数据,用于与 renderItem 组合渲染;泛型 I 会推断出每一项的数据类型。 也可不传 dataSource,直接以 SwiperItem 作为 children 渲染。
否-渲染列表每一项,item 类型由 dataSource 的泛型 I 推断
否-current 改变时触发
否-动画结束时触发

合法值

SwiperComponentChangeEvent.source
值说明最低版本
''非自动播放或用户滑动引起的切换,如通过 current 属性修改当前滑块-
autoplay自动播放引起的切换-
touch用户滑动引起的切换-
SwiperChangeDetail.source
值说明最低版本
''非自动播放或用户滑动引起的切换,如通过 current 属性修改当前滑块-
autoplay自动播放引起的切换-
touch用户滑动引起的切换-
SwiperComponentAfterChangeEvent.source
值说明最低版本
''非自动播放或用户滑动引起的切换,如通过 current 属性修改当前滑块-
autoplay自动播放引起的切换-
touch用户滑动引起的切换-

示例代码

基础用法

import React, { useState } from 'react';
import { Swiper, SwiperItem, View, Button, Text } from '@ray-js/ray';

export default function BasicSwiper() {
  const [current, setCurrent] = useState(0);
  const colors = ['#1890ff', '#52c41a', '#faad14'];

  return (
    <View>
      <Swiper
        style={{ width: '702rpx', height: '200rpx' }}
        dots
        current={current}
        onChange={(e) => setCurrent(e.current)}
      >
        {colors.map((color, index) => (
          <SwiperItem key={index}>
            <View
              style={{
                width: '100%',
                height: '100%',
                backgroundColor: color,
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
              }}
            >
              <Text style={{ color: '#fff' }}>第 {index + 1} 页</Text>
            </View>
          </SwiperItem>
        ))}
      </Swiper>
      <Button
        style={{ marginTop: '20rpx' }}
        onClick={() => setCurrent((current + 1) % 3)}
      >
        切换到第 {(current + 1) % 3 + 1} 页
      </Button>
    </View>
  );
}

自动轮播与 dataSource

import React, { useState } from 'react';
import { Swiper, View, Text } from '@ray-js/ray';

export default function AutoplaySwiper() {
  const [current, setCurrent] = useState(0);
  const dataSource = [
    { id: '1', color: '#1890ff', label: '轮播一' },
    { id: '2', color: '#52c41a', label: '轮播二' },
    { id: '3', color: '#faad14', label: '轮播三' },
  ];

  return (
    <Swiper
      style={{ width: '702rpx', height: '200rpx' }}
      autoplay
      interval={2500}
      circular
      dots
      current={current}
      dataSource={dataSource}
      renderItem={(item) => (
        <View
          style={{
            width: '100%',
            height: '100%',
            backgroundColor: item.color,
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
          }}
        >
          <Text style={{ color: '#fff', fontSize: '32rpx' }}>{item.label}</Text>
        </View>
      )}
      onChange={(e) => setCurrent(e.current)}
    />
  );
}

纵向轮播与指示点颜色

import React, { useState } from 'react';
import { Swiper, View, Text } from '@ray-js/ray';

export default function VerticalSwiper() {
  const [current, setCurrent] = useState(0);
  const dataSource = [
    { id: '1', color: '#722ed1', label: '纵向一' },
    { id: '2', color: '#eb2f96', label: '纵向二' },
    { id: '3', color: '#13c2c2', label: '纵向三' },
  ];

  return (
    <Swiper
      style={{ width: '702rpx', height: '300rpx' }}
      vertical
      dots
      dotColor="rgba(255,255,255,0.4)"
      dotActiveColor="#fff"
      duration={400}
      current={current}
      dataSource={dataSource}
      renderItem={(item) => (
        <View
          style={{
            width: '100%',
            height: '100%',
            backgroundColor: item.color,
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
          }}
        >
          <Text style={{ color: '#fff', fontSize: '32rpx' }}>{item.label}</Text>
        </View>
      )}
      onChange={(e) => setCurrent(e.current)}
      onAfterChange={(e) => console.log('动画结束:', e.current)}
    />
  );
}