Swiper/SwiperItem 轮播

import {
  TaSwiper,
  TaGroup,
  TaImage,
  showToast,
  SwiperOnActiveIndexChange,
  SwiperOnAnimated
} from '@/index'

export default function ExpSwiper() {
  function renderItems() {
    return (
      <>
        {[1, 2, 3, 4].map((item, index) => {
          return (
            <TaSwiper.Item key={item}>
              <div
                className={`exp-swiper-box-item ${
                  index % 2 === 1 ? 'even' : 'odd'
                }`}
              >
                {item}
              </div>
            </TaSwiper.Item>
          )
        })}
      </>
    )
  }

  function renderImages() {
    return (
      <>
        {[
          'https://cdn.fox2.cn/vfox/swiper/regular-1.jpg',
          'https://cdn.fox2.cn/vfox/swiper/regular-2.jpg',
          'https://cdn.fox2.cn/vfox/swiper/regular-3.jpg'
        ].map(item => {
          return (
            <TaSwiper.Item key={item}>
              <TaImage className="exp-swiper-image" src={item} />
            </TaSwiper.Item>
          )
        })}
      </>
    )
  }

  const onChange: SwiperOnActiveIndexChange = activeIndex => {
    showToast(`change 到第 ${activeIndex + 1}`)
    console.log('change', activeIndex)
  }

  const onAnimated: SwiperOnAnimated = activeIndex => {
    showToast(`${activeIndex + 1} 张 animated`)
    console.log('animated', activeIndex)
  }

  return (
    <>
      <TaGroup title="基础用法">
        <TaSwiper className="exp-swiper-box">{renderItems()}</TaSwiper>
      </TaGroup>
      <TaGroup title="显示面板指示点 indicatorDots=true">
        <TaSwiper className="exp-swiper-box" indicatorDots>
          {renderImages()}
        </TaSwiper>
      </TaGroup>
      <TaGroup title="显示切换按钮 navigation-buttons=true">
        <TaSwiper className="exp-swiper-box" navigationButtons>
          {renderImages()}
        </TaSwiper>
      </TaGroup>
      <TaGroup title="循环展示 initialCircular=true">
        <TaSwiper className="exp-swiper-box" indicatorDots initialCircular>
          {renderItems()}
        </TaSwiper>
      </TaGroup>
      <TaGroup title="垂直方向 initialVertical=true">
        <TaSwiper className="exp-swiper-box" indicatorDots initialVertical>
          {renderItems()}
        </TaSwiper>
      </TaGroup>
      <TaGroup title="更改指示点颜色">
        <TaSwiper
          className="exp-swiper-box"
          indicatorDots
          indicatorColor="rgba(255, 255, 255, 0.5)"
          indicatorActiveColor="#ff4d4f"
        >
          {renderItems()}
        </TaSwiper>
      </TaGroup>
      <TaGroup title="自动轮播(切换时长设置为3000ms)">
        <TaSwiper
          className="exp-swiper-box"
          indicatorDots
          autoplay
          interval="3000"
        >
          {renderImages()}
        </TaSwiper>
      </TaGroup>
      <TaGroup title="滑动过程时长(设置为2000ms)">
        <TaSwiper className="exp-swiper-box" indicatorDots duration="2000">
          {renderImages()}
        </TaSwiper>
      </TaGroup>
      <TaGroup title="事件监听(change/animated/click)">
        <TaSwiper
          className="exp-swiper-box"
          indicatorDots
          onActiveIndexChange={onChange}
          onAnimated={onAnimated}
          onClick={() => showToast('click 触发')}
        >
          {renderItems()}
        </TaSwiper>
      </TaGroup>
    </>
  )
}

Import

import { TaSwiper, TaSwiperItem } from 'tantalum-ui-mobile-react'

具体的引入方式可以参考引入组件

Import Type

组件导出的类型定义:

import type {
  SwiperOnActiveIndexChange,
  SwiperOnAnimated,
  SwiperRef
} from 'tantalum-ui-mobile-react'

Swiper Props

属性类型默认值必填说明
activeIndexnumber0当前所在滑块的 index
indicatorDotsbooleanfalse是否显示面板指示点
indicatorColorcolor'rgba(0, 0, 0, 0.4)'指示点颜色
indicatorActiveColorcolor'rgba(255, 255, 255, 0.8)'当前选中的指示点颜色
autoplaybooleanfalse是否自动切换
intervalnumber5000自动切换时间间隔
durationnumber滑动动画时长,没有设置时使用内置调优时长
initialCircularbooleanfalse初始设置是否循环切换
initialVerticalbooleanfalse初始设置是否为纵向滑动
navigationButtonsbooleanfalse是否展示上一页/下一页按钮

Swiper Events

事件描述回调函数参数TypeScript 函数
onActiveIndexChange切换时触发( activeIndex: number, fromIndex: number )SwiperOnActiveIndexChange
onAnimated动画结束时触发( activeIndex: number, fromIndex: number )SwiperOnAnimated
onClick点击时触发,为了区分滑动情况

Swiper Slots

children

注:其中只可放置 SwiperItem 组件,否则会导致未定义的行为。

<TaSwiper>
  <TaSwiper.Item>
    <TaImage src="a.jpg" />
  </TaSwiper.Item>
  <TaSwiper.Item>
    <TaImage src="b.jpg" />
  </TaSwiper.Item>
  ...
</TaSwiper>

SwiperItem Slots

children

<TaSwiper.Item>
  <TaImage src="b.jpg" />
</TaSwiper.Item>

Methods

interface SwiperRef {
  swipeTo: (newIndex: number) => void
  prev: () => void
  next: () => void
}
方法名说明
swipeTo切换到指定 index 的 Item
prev切换到上一个 Item,如果当前在第一个 ,则循环到最后一个
next切换到下一个 Item,如果当前在最后一个,则循环到第一个