swiper 滑块视图容器

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

属性说明

属性名类型默认值必填说明最低版本
indicator-dotsBooleanfalse是否显示面板指示点-
indicator-colorColorrgba(0, 0, 0, .3)指示点颜色-
indicator-active-colorColor#333当前选中的指示点颜色-
autoplayBooleanfalse是否自动切换-
currentNumber0当前所在页面的 index-
current-item-idString当前所在滑块的 item-id,不能与 current 被同时指定1.11 低版本请做兼容性处理
intervalNumber5000自动切换时间间隔,单位ms-
durationNumber500滑动动画时长,单位ms-
circularBooleanfalse是否采用衔接滑动-
verticalBooleanfalse滑动方向是否为纵向-
previous-marginString"0px"前边距,可用于露出前一项的一小部分,支持px和rpx1.11低版本请做兼容性处理
next-marginString"0px"后边距,可用于露出后一项的一小部分,支持px和rpx1.11低版本请做兼容性处理
display-multiple-itemsNumber1同时显示的滑块数量1.11低版本请做兼容性处理
bindchangeEventHandlecurrent 改变时会触发 change 事件,event.detail = {current: current, source: source}-
bindanimationfinishEventHandle动画结束时会触发 animationfinish 事件,event.detail 同上1.11低版本请做兼容性处理

change 事件 source 返回值

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

说明
autoplay自动播放导致的swiper切换
touch用户划动导致的swiper切换
""其他原因将返回空字符串

示例

在开发者工具中预览效果

扫码体验

swiper 滑块视图容器 - 图1请使用百度APP扫码

代码示例 :滑块滑动

  1. <view class="wrap">
  2. <view class="title"></view>
  3. <view class="swiper-wrap">
  4. <swiper
  5. indicator-color="rgba(0,0,0,0.30)"
  6. indicator-active-color="#fff"
  7. duration="{{switchDuration}}"
  8. interval="{{autoPlayInterval}}"
  9. autoplay="{{switchAutoPlayStatus}}"
  10. indicator-dots="{{switchIndicateStatus}}"
  11. vertical="false"
  12. circular="true"
  13. current="0"
  14. current-item-id="0"
  15. previous-margin="0px"
  16. next-margin="0px"
  17. display-multiple-items="1"
  18. bind:change="swiperChange"
  19. bind:animationfinish="animationfinish"
  20. >
  21. <block s-for="item in items">
  22. <swiper-item class="{{item.className}}">
  23. <view class="item">{{item.value}}</view>
  24. </swiper-item>
  25. </block>
  26. </swiper>
  27. </view>
  28. <view class="switch-wrap">
  29. <view>
  30. <text>指示器</text>
  31. <switch checked="{{switchIndicateStatus}}" bind:change="switchIndicate" class="switch"></switch>
  32. </view>
  33. <view>
  34. <text>自动播放</text>
  35. <switch checked="{{switchAutoPlayStatus}}" bind:change="switchAutoPlay" class="switch"></switch>
  36. </view>
  37. </view>
  38. <view class="slider-wrap">
  39. <view>
  40. <view class="slider-title-time">
  41. <text class="slider-title">幻灯片切换时长</text>
  42. <text class="slider-time">{{switchDuration}}ms</text>
  43. </view>
  44. <slider min="300" max="1500" value="{{switchDuration}}" bind:change="changeSwitchDuration"></slider>
  45. </view>
  46. <view>
  47. <view class="slider-title-time">
  48. <text class="slider-title">自动播放间隔时长</text>
  49. <text class="slider-time">{{autoPlayInterval}}ms</text>
  50. </view>
  51. <slider min="1000" max="5000" value="{{autoPlayInterval}}" bind:change="changeAutoPlayInterval"></slider>
  52. </view>
  53. </view>
  54. </view>
  1. Page({
  2. data: {
  3. items: [
  4. {
  5. className: 'color-a',
  6. value: 'A'
  7. }, {
  8. className: 'color-b',
  9. value: 'B'
  10. }, {
  11. className: 'color-c',
  12. value: 'C'
  13. }
  14. ],
  15. imgUrls: [
  16. 'https://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
  17. 'https://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
  18. 'https://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
  19. 'https://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg'
  20. ],
  21. current: 0,
  22. switchIndicateStatus: true,
  23. switchAutoPlayStatus: false,
  24. switchDuration: 500,
  25. autoPlayInterval: 2000
  26. },
  27. swiperChange(e) {
  28. console.log('swiperChange:', e.detail);
  29. },
  30. switchIndicate() {
  31. this.setData({
  32. switchIndicateStatus: !this.getData('switchIndicateStatus')
  33. });
  34. },
  35. switchAutoPlay() {
  36. this.setData({
  37. switchAutoPlayStatus: !this.getData('switchAutoPlayStatus')
  38. });
  39. },
  40. changeSwitchDuration(e) {
  41. this.setData({
  42. switchDuration: e.detail.value
  43. });
  44. },
  45. changeAutoPlayInterval(e) {
  46. this.setData({
  47. autoPlayInterval: e.detail.value
  48. });
  49. },
  50. animationfinish() {
  51. console.log('animationfinish');
  52. }
  53. });

Bug & Tip

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

参考示例

参考示例 1:用于实现顶部标签切换

在开发者工具中预览效果

  1. <view class="container">
  2. <!-- 顶部导航 -->
  3. <view class="swiper-tab">
  4. <view class="tab-item {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swiperNav">Tab1</view>
  5. <view class="tab-item {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swiperNav">Tab2</view>
  6. <view class="tab-item {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swiperNav">Tab3</view>
  7. </view>
  8. <!-- 顶部导航对应的内容 -->
  9. <swiper class="swiper" current="{{currentTab}}" duration="200" bindchange="swiperChange">
  10. <swiper-item>
  11. <!-- 页面可使用自定义组件编写 -->
  12. <view>我是tab1</view>
  13. </swiper-item>
  14. <swiper-item>
  15. <view>我是tab2</view>
  16. </swiper-item>
  17. <swiper-item>
  18. <view>我是tab3</view>
  19. </swiper-item>
  20. </swiper>
  21. </view>
  1. Page({
  2. data: {
  3. currentTab: 0,
  4. },
  5. swiperNav(e) {
  6. console.log(e);
  7. if (this.data.currentTab === e.target.dataset.current) {
  8. return false;
  9. } else {
  10. this.setData({
  11. currentTab: e.target.dataset.current,
  12. })
  13. }
  14. },
  15. swiperChange: function (e) {
  16. console.log(e);
  17. this.setData({
  18. currentTab: e.detail.current,
  19. })
  20. }
  21. })
  1. .swiper-tab {
  2. display: flex;
  3. flex-direction: row;
  4. line-height: 80rpx;
  5. border-bottom: 1rpx solid #f5f5f5;
  6. }
  7. .tab-item {
  8. width: 33.3%;
  9. text-align: center;
  10. font-size: .16rem;
  11. color: rgb(116, 113, 113);
  12. }
  13. .swiper {
  14. height: 1100px;
  15. background: #dfdfdf;
  16. }
  17. .on {
  18. color: #5B9FFF;
  19. border-bottom: 1px solid #5B9FFF;
  20. padding-bottom: 2px
  21. }

参考示例 2: 自定义底部切换圆点

在开发者工具中预览效果

  1. <view class="swiper-wrap">
  2. <swiper autoplay="auto" interval="5000" duration="500" current="{{swiperCurrent}}" bindchange="swiperChange" class="swiper">
  3. <swiper-item s-for="{{slider}}">
  4. <image src="{{item.imageUrl}}" class="img"></image>
  5. </swiper-item>
  6. </swiper>
  7. <view class="dots">
  8. <view s-for="{{slider}}" class="dot {{index == swiperCurrent ? ' active' : ''}}"></view>
  9. </view>
  10. </view>
Page({
    data: {
        slider: [{
            imageUrl: 'https://b.bdstatic.com/miniapp/image/hypnosis.jpeg'
        }, {
            imageUrl: 'https://b.bdstatic.com/miniapp/images/hypnosis.jpeg'
        }, {
            imageUrl: 'https://b.bdstatic.com/miniapp/image/airCaptain.jpeg'
        }],
        swiperCurrent: 0
    },
    swiperChange(e) {
        this.setData({
            swiperCurrent: e.detail.current
        })
    }
})
.swiper-wrap{
    position: relative;
}
.swiper-wrap .swiper{
    height: 300rpx;
}
.swiper-wrap .swiper .img{
    width: 100%;
    height: 100%;
}
.swiper-wrap .dots{
    position: absolute;
    right: 20rpx;
    bottom: 20rpx;
    display: flex;
    justify-content: center;
}
.swiper-wrap .dots .dot{
    margin: 0 8rpx;
    width: 14rpx;
    height: 14rpx;
    background: #fff;
    border-radius: 8rpx;
    transition: all .6s;
}

.swiper-wrap .dots .dot.active{
    width: 24rpx;
    background: #38f;
}