后浪云百度小程序教程:swiper 滑块视图容器

  • swiper 滑块视图容器
    • 属性说明
      • change 事件 source 返回值
    • 示例
      • 代码示例 1:
      • 代码示例 2:自定义底部切换圆点
      • 代码示例 3:模拟 tabs 组件功能
    • Bug & Tip
    • 常见问题
      • Q:swiper 的面板指示点能自定义样式吗?

    swiper 滑块视图容器

    解释:滑块视图容器。内部只允许使用 swiper-item 组件描述滑块内容,否则会导致未定义的行为。

    属性说明

    属性名 类型 默认值 必填 说明 最低版本

    indicator-dots

    Boolean

    false

    是否显示面板指示点

    -

    indicator-color

    Color

    rgba(0, 0, 0, .3)

    指示点颜色

    -

    indicator-active-color

    Color

    #333

    当前选中的指示点颜色

    -

    autoplay

    Boolean

    false

    是否自动切换

    -

    current

    Number

    0

    当前所在页面的 index

    -

    current-item-id

    String

    当前所在滑块的 item-id ,不能与 current 被同时指定

    1.11
    低版本请做兼容性处理

    interval

    Number

    5000

    自动切换时间间隔(单位:ms)

    -

    duration

    Number

    500

    滑动动画时长(单位:ms)

    -

    circular

    Boolean

    false

    是否采用衔接滑动

    -

    vertical

    Boolean

    false

    滑动方向是否为纵向

    -

    previous-margin

    String

    “0px”

    前边距,可用于露出前一项的一小部分,支持 px 和 rpx

    1.11
    低版本请做兼容性处理

    next-margin

    String

    “0px”

    后边距,可用于露出后一项的一小部分,支持 px 和 rpx

    1.11
    低版本请做兼容性处理

    display-multiple-items

    Number

    1

    同时显示的滑块数量

    1.11
    低版本请做兼容性处理

    bindchange

    EventHandle

    current 改变时会触发 change 事件,event.detail = {current: current, source: source}

    -

    bindanimationfinish

    EventHandle

    动画结束时会触发 animationfinish 事件,event.detail 同上

    1.11
    低版本请做兼容性处理

    change 事件 source 返回值

    change 事件中的 source 字段,表示触发 change 事件的原因,可能值如下:

    说明

    autoplay

    自动播放导致的 swiper 切换

    touch

    用户滑动导致的 swiper 切换

    “”

    其他原因将返回空字符串

    示例

    跳转编辑工具

    在开发者工具中打开

    在 WEB IDE 中打开

    扫码体验

    代码示例

    后浪云百度小程序教程:swiper 滑块视图容器插图

    请使用百度APP扫码

    代码示例 1:

    • SWAN
    • JS
    • CSS
     
     
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <swiper
    4. class="swiper"
    5. indicator-dots="{{switchIndicateStatus}}"
    6. indicator-color="rgba(0,0,0,0.30)"
    7. indicator-active-color="#fff"
    8. autoplay="{{switchAutoPlayStatus}}"
    9. current="0"
    10. current-item-id="0"
    11. interval="{{autoPlayInterval}}"
    12. duration="{{switchDuration}}"
    13. circular="true"
    14. vertical="{{switchVerticalStatus}}"
    15. previous-margin="0px"
    16. next-margin="0px"
    17. display-multiple-items="1"
    18. bind:change="swiperChange"
    19. bind:animationfinish="animationfinish">
    20. <swiper-item
    21. s-for="item in swiperList"
    22. item-id="{{itemId}}"
    23. class="{{item.className}}">
    24. <view class="swiper-item">{{item.value}}</view>
    25. </swiper-item>
    26. </swiper>
    27. <view class="item-scroll border-bottom">
    28. <text class="switch-text-before">指示点</text>
    29. <switch
    30. class="init-switch"
    31. checked="{{switchIndicateStatus}}"
    32. bind:change="switchIndicate">
    33. </switch>
    34. </view>
    35. <view class="item-scroll border-bottom">
    36. <text class="switch-text-before">自动切换</text>
    37. <switch
    38. checked="{{switchAutoPlayStatus}}"
    39. bind:change="switchAutoPlay"
    40. class="init-switch">
    41. </switch>
    42. </view>
    43. <view class="item-scroll">
    44. <text class="switch-text-before">纵向滑动</text>
    45. <switch
    46. checked="{{switchVerticalStatus}}"
    47. bind:change="switchVertical"
    48. class="init-switch">
    49. </switch>
    50. </view>
    51. </view>
    52. <view class="card-area">
    53. <view class="top-description border-bottom">
    54. <view>滑块切换时长</view>
    55. <view>{{switchDuration}}ms</view>
    56. </view>
    57. <slider
    58. class="slider"
    59. min="300"
    60. max="1500"
    61. value="{{switchDuration}}"
    62. bind:change="changeSwitchDuration">
    63. </slider>
    64. </view>
    65. <view class="card-area">
    66. <view class="top-description border-bottom">
    67. <view>自动切换时间间隔</view>
    68. <view>{{autoPlayInterval}}ms</view>
    69. </view>
    70. <slider
    71. class="slider"
    72. min="1000"
    73. max="5000"
    74. value="{{autoPlayInterval}}"
    75. bind:change="changeAutoPlayInterval">
    76. </slider>
    77. </view>
    78. </view>

    设计指南

    建议滑块视图数量控制在 2-5 个。
    建议滑块切换时长不低于 500ms ,自动切换时间间隔不高于 5000ms 。

    代码示例 2:自定义底部切换圆点

    • SWAN
    • JS
    • CSS
     
     
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <view class="top-description">自定义底部切换圆点</view>
    4. <view class="swiper-wrap">
    5. <swiper
    6. class="swiper-custom"
    7. autoplay="auto"
    8. interval="3000"
    9. duration="500"
    10. current="{{swiperCurrent}}"
    11. bindchange="swiperChangeCustom">
    12. <swiper-item s-for="{{slider}}">
    13. <image class="swiper-img" src="{{item.imageUrl}}"></image>
    14. </swiper-item>
    15. </swiper>
    16. <view class="dots">
    17. <view s-for="{{slider}}" class="dot {{index == swiperCurrent ? ' active' : ''}}"></view>
    18. </view>
    19. </view>
    20. </view>
    21. </view>

    代码示例 3:模拟 tabs 组件功能

    • SWAN
    • JS
    • CSS
     
     
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <view class="top-description">模拟 tabs 组件功能</view>
    4. <!-- 顶部导航 -->
    5. <view class="swiper-tab">
    6. <view class="tab-item {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swiperNav">全部</view>
    7. <view class="tab-item {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swiperNav">服务通知</view>
    8. <view class="tab-item {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swiperNav">系统通知</view>
    9. <view class="tab-item {{currentTab==3 ? 'on' : ''}}" data-current="3" bindtap="swiperNav">评论</view>
    10. <view class="tab-item {{currentTab==4 ? 'on' : ''}}" data-current="4" bindtap="swiperNav">其他</view>
    11. </view>
    12. <!-- 顶部导航对应的内容 -->
    13. <swiper class="swiper-content" current="{{currentTab}}" duration="200">
    14. <swiper-item>
    15. <view>我是全部</view>
    16. </swiper-item>
    17. <swiper-item>
    18. <view>我是服务通知</view>
    19. </swiper-item>
    20. <swiper-item>
    21. <view>我是系统通知</view>
    22. </swiper-item>
    23. <swiper-item>
    24. <view>我是评论</view>
    25. </swiper-item>
    26. <swiper-item>
    27. <view>我是其他</view>
    28. </swiper-item>
    29. </swiper>
    30. </view>
    31. </view>

    Bug & Tip

    • Tip:如果在 bindchange 的事件回调函数中使用 setData 改变 current 值,则会导致 setData 被重复调用,因而通常情况下请在改变 current 值前检测 source 字段来判断是否是由于用户触摸引起的。
    • Tip:组件内部只可放置 swiper-item 组件,否则会导致未定义的行为。

    常见问题

    Q:swiper 的面板指示点能自定义样式吗?

    A:参见属性说明,可以去掉 dot 显示之后,自己定义 dot 样式。具体代码可参见上方代码示例 2 。

    THE END