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
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
activeIndex | number | 0 | 否 | 当前所在滑块的 index |
indicatorDots | boolean | false | 否 | 是否显示面板指示点 |
indicatorColor | color | 'rgba(0, 0, 0, 0.4)' | 否 | 指示点颜色 |
indicatorActiveColor | color | 'rgba(255, 255, 255, 0.8)' | 否 | 当前选中的指示点颜色 |
autoplay | boolean | false | 否 | 是否自动切换 |
interval | number | 5000 | 否 | 自动切换时间间隔 |
duration | number | 否 | 滑动动画时长,没有设置时使用内置调优时长 | |
initialCircular | boolean | false | 否 | 初始设置是否循环切换 |
initialVertical | boolean | false | 否 | 初始设置是否为纵向滑动 |
navigationButtons | boolean | false | 否 | 是否展示上一页/下一页按钮 |
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,如果当前在最后一个,则循环到第一个 |