waft>=0.7.x waft-ui>=0.2.x

引入

  1. {
  2. "usingComponents": {
  3. "x-swiper":"waft-ui/assembly/swiper/swiper"
  4. }
  5. }

代码示例

  1. <x-swiper ref="swiper" loop="{{ true }}" autoplay="{{ true }}" count="{{ list.length }}" defaultIndex="{{ 0 }}" indicatorsPosition="right">
  2. <view class="swiper-item" a:for="{{ list }}">{{ item.name }}</view>
  3. </x-swiper>

refs 代码示例

  • axml 代码

    1. <x-swiper ref="component_ref" count="{{ 4 }}" loop="{{ true }}" autoplay="{{ true }}" defaultIndex="{{ 0 }}" indicatorsPosition="right">
    2. <view style="width: 100%;height: 100%">1</view>
    3. <view style="width: 100%;height: 100%">2</view>
    4. <view style="width: 100%;height: 100%">3</view>
    5. <view style="width: 100%;height: 100%">4</view>
    6. </x-swiper>
  • TS 代码 ```typescript import {Page, Props } from ‘waft’; // 不然不能成功转成 Swiper 实例, 调用 Swiper 方法 import { Swiper } from ‘waft-ui’; import { refs } from ‘waft-ui-common’;

export Test extends Page { constructor(props: Props) { super(props);

  1. this.addEventListener('xxx', () => {
  2. // 注意, refs 实例是在组件 willMount 生命周期挂载的, 注意调用时机, 早于组件 willMount
  3. // 生命周期之前获取组件实例可能会获取不到
  4. if (refs.get('component_ref') instanceof Swiper) {
  5. const instance = refs.get('component_ref') as Swiper;
  6. // 控制轮播图 上一页
  7. instance.prev();
  8. // 控制轮播图 下一页
  9. instance.next();
  10. // 控制轮播图 跳到指定位置
  11. instance.swipeTo(3);
  12. }
  13. })

} }

  1. <a name="fAiKz"></a>
  2. ### 效果展示
  3. ![swiper.png](https://cdn.nlark.com/yuque/0/2021/png/327148/1630577172846-fb2dc307-61e0-4995-b5cd-fcf243c544a8.png#clientId=ud28402b3-2edc-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=ue1ea841d&margin=%5Bobject%20Object%5D&name=swiper.png&originHeight=800&originWidth=1280&originalType=binary&ratio=1&rotation=0&showTitle=false&size=21339&status=done&style=shadow&taskId=u455bd159-40df-43dc-960e-1f6b980748b&title=)
  4. <a name="bhxMx"></a>
  5. ### 组件样式
  6. <a name="A5dzP"></a>
  7. #### axml
  8. ```html
  9. <view class="test-swiper">
  10. <x-nav-bar showArrow="{{ true }}" title="测试 swiper 组件"></x-nav-bar>
  11. <view style="height: 30rpx"></view>
  12. <x-scroll>
  13. <view class="content">
  14. <h3>指示器位置</h3>
  15. <view class="swiper">
  16. <x-swiper ref="swiper" loop="{{ true }}" autoplay="{{ true }}" count="{{ list.length }}" defaultIndex="{{ 0 }}" indicatorsPosition="right">
  17. <view class="swiper-item" a:for="{{ list }}">{{ item.name }}</view>
  18. </x-swiper>
  19. </view>
  20. <h3>自动轮播</h3>
  21. <view class="swiper">
  22. <x-swiper ref="swiper" loop="{{ true }}" autoplay="{{ true }}" count="{{ list.length }}" defaultIndex="{{ 0 }}">
  23. <view class="swiper-item" a:for="{{ list }}">{{ item.name }}</view>
  24. </x-swiper>
  25. </view>
  26. <h3>手动控制</h3>
  27. <view class="btns">
  28. <view class="btn" onTap="handlePrev">上一个</view>
  29. <view class="btn" onTap="next">下一个</view>
  30. </view>
  31. <view class="swiper">
  32. <x-swiper ref="swiper" loop="{{ true }}" autoplay="{{ false }}" count="{{ list.length }}" defaultIndex="{{ 3 }}">
  33. <view class="swiper-item" a:for="{{ list }}">{{ item.name }}</view>
  34. </x-swiper>
  35. </view>
  36. <view style="height: 100rpx;background-color: #00b3ff"></view>
  37. </view>
  38. </x-scroll>
  39. </view>

css

  1. .test-swiper {
  2. width: 100vw;
  3. height: 100vh;
  4. background-color: #E6F2FF;
  5. display: flex;
  6. flex-direction: column;
  7. justify-content: flex-start;
  8. align-items: flex-start;
  9. }
  10. .content {
  11. flex: 1;
  12. width: 100%;
  13. display: flex;
  14. flex-direction: column;
  15. justify-content: flex-start;
  16. align-items: center;
  17. }
  18. .swiper {
  19. width: 800rpx;
  20. height: 500rpx;
  21. }
  22. .swiper-item {
  23. width: 100%;
  24. height: 100%;
  25. display: flex;
  26. justify-content: center;
  27. align-items: center;
  28. background-color: #00b3ff;
  29. color: #FFFFFF;
  30. font-size: 48rpx;
  31. }
  32. .btns {
  33. display: flex;
  34. flex-direction: row;
  35. justify-content: flex-start;
  36. align-items: flex-start;
  37. flex-wrap: wrap;
  38. margin-bottom: 20rpx;
  39. margin-top: 20rpx;
  40. }
  41. .btn {
  42. height: 40rpx;
  43. line-height: 40rpx;
  44. background-color: #00b3ff;
  45. border-radius: 5rpx;
  46. padding: 0 10rpx;
  47. margin-right: 20rpx;
  48. }

json

  1. {
  2. "usingComponents": {
  3. "x-nav-bar":"../assembly/nav-bar/nav-bar",
  4. "x-cell":"../assembly/cell/cell",
  5. "x-swiper": "../assembly/swiper/swiper",
  6. "x-scroll": "../assembly/scroll/scroll"
  7. },
  8. "state": {
  9. "list": [
  10. {
  11. "name": 1
  12. },
  13. {
  14. "name": 2
  15. },
  16. {
  17. "name": 3
  18. },
  19. {
  20. "name": 4
  21. },
  22. {
  23. "name": 5
  24. }
  25. ]
  26. }
  27. }

ts

  1. import {Page, Props, Element, ComponentProps, Component} from 'waft';
  2. import { JSON, JSONObject } from 'waft-json';
  3. import { Swiper } from '../../../assembly/swiper/swiper';
  4. import { refs } from 'waft-ui-common';
  5. export class TestSwiper extends Page {
  6. constructor(props: Props) {
  7. super(props);
  8. this.addEventListener('handlePrev', () => {
  9. console.log('====### 进入到了 上一个');
  10. if (refs.get('swiper')) {
  11. console.log('====### swiper 有值');
  12. if (refs.get('swiper') instanceof Swiper) {
  13. console.log('即将获取值');
  14. const target = refs.get('swiper') as Swiper;
  15. console.log('nodeId 是' + target.nodeId.toString());
  16. target.prev();
  17. }
  18. }
  19. });
  20. this.addEventListener('next', () => {
  21. const target = refs.get('swiper') as Swiper;
  22. target.next();
  23. });
  24. }
  25. }

API

  • props | 参数 | 说明 | 类型 | 默认值 | | —- | —- | —- | —- | | autoplay | 是否自动播放 | boolean | true | | interval | 轮播间隔, 单位 ms | number | 2000 | | loop | 是否循环播放 | boolean | true | | count | 轮播的子节点数量, 必传!!!! | number | 1 | | active | 手动控制索引值 | number |
    | | defaultIndex | 初始位置索引值 | number | 0 | | showIndicators | 是否显示指示器 | boolean | true | | indicatorsPosition | 指示器位置 支持 center left right | string | center |

  • Events | 事件名 | 说明 | 回调参数 | | —- | —- | —- | | change | 每一页轮播结束后触发 | index 当前页的索引 | | touchStart | 开始滑动 swiper 的时候触发 | index 当前页的索引 |

  • slot | 名称 | 说明 | | —- | —- | | default | 轮播内容 ⚠️ 内容不会变化 | | indicator | 自定义指示器 ⚠️ 具名插槽不能设置默认内容 |

  • refs | 方法名 | 参数 | 说明 | | —- | —- | —- | | prev |
    | 切换到上一轮播 | | next |
    | 切换到下一轮播 | | swipeTo | index | 切换到指定的轮播 |