智能小程序 Ray 开发实践——视图容器组件 Swiper 介绍

本文介绍了如何在React中使用Swiper库创建轮播效果,包括设置属性如自动切换、指示点颜色、事件处理等,并提供了示例代码。同时提及了免费领取开发资源以体验涂鸦MiniApp小程序开发。
摘要由CSDN通过智能技术生成

Swiper

幻灯片、轮播容器,使用时必须指定整体高度。

导入

import { Swiper } from '@ray-js/ray';

属性说明

属性类型默认值说明支持平台
autoplaybooleanfalse是否自动切换涂鸦、微信
circularbooleanfalse是否采用衔接滑动涂鸦、微信
currentnumber0当前所在滑块的 index涂鸦、微信
dotActiveColorstring#666666当前选中的指示点颜色涂鸦、微信
dotColorstring#333333指示点颜色涂鸦、微信
dotsbooleanfalse是否显示面板指示点涂鸦、微信
durationnumber500滑动动画时长涂鸦、微信
intervalnumber5000自动切换时间间隔涂鸦、微信
verticalbooleanfalse滑动方向是否为纵向涂鸦、微信
dataSourceP[]数据源涂鸦、微信
renderItemfunc(P)渲染数据成员涂鸦、微信
onChangefunc(event)current 改变时, event.detail = {current, source}涂鸦、微信
onAfterChangefunc(event)动画结束时触发,event.detail 同上涂鸦、微信

示例代码

  • index.module.less
.swiper {
  width: 702rpx;
  height: 200rpx;
}
.swiperItem {
  width: 702rpx;
  height: 200rpx;
  display: block;
}
.nested {
  width: 702rpx;
  height: 400rpx;
}
.nestedItem {
  width: 702rpx;
  height: 400rpx;
}
.nestedSwiper {
  width: 702rpx;
  height: 200rpx;
}

基本用法
import React from 'react';
 
import { Button, View, Swiper } from '@ray-js/components';
 
import styles from './index.module.less';
 
function randomColor() {
  const letters = '0123456789ABCDEF';
  let color = '#';
  for (let i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  }
  return color;
}
 
export default function Demo() {
  const [current, setCurrent] = React.useState(0);
 
  const getRandomCurrent = React.useCallback(() => {
    let newCurrent = (current + Math.ceil(Math.random() * 3)) % 3;
    while (newCurrent === current) {
      newCurrent = (current + Math.ceil(Math.random() * 3)) % 3;
    }
    return newCurrent;
  }, [current]);
 
  return (
    <React.Fragment>
      <Swiper
        current={current}
        dots
        className={styles.swiper}
        dataSource={[randomColor(), randomColor(), randomColor()]}
        renderItem={(color: string) => {
          return (
            <View
              className={styles.swiperItem}
              style={{ backgroundColor: color }}
            />
          );
        }}
        onChange={(event) => {
          const { current } = event;
          setCurrent(current);
        }}
      />
      <Button
        onClick={() => {
          const nc = getRandomCurrent();
          setCurrent(nc);
          console.log({ title: '切换到第 ' + nc + ' 个元素', icon: 'none' });
        }}
      >
        手动切换
      </Button>
    </React.Fragment>
  );
}

👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IoT砖家涂拉拉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值