前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序官方组件展示之视图容器swiper源码

微信小程序官方组件展示之视图容器swiper源码

作者头像
软件事业部
发布2022-08-16 11:16:36
7050
发布2022-08-16 11:16:36
举报

以下将展示微信小程序之视图容器swiper源码官方组件能力,组件样式仅供参考,开发者可根据自身需求定义组件样式,具体属性参数详见小程序开发文档。

功能描述:

滑块视图容器。其中只可放置swiper-item组件,否则会导致未定义的行为。

属性说明:

WebView

属性

类型

默认值

必填

说明

最低版本

indicator-dots

boolean

FALSE

是否显示面板指示点

1.0.0

indicator-color

color

rgba(0, 0, 0, .3)

指示点颜色

1.1.0

indicator-active-color

color

#000000

当前选中的指示点颜色

1.1.0

autoplay

boolean

FALSE

是否自动切换

1.0.0

current

number

0

当前所在滑块的 index

1.0.0

interval

number

5000

自动切换时间间隔

1.0.0

duration

number

500

滑动动画时长

1.0.0

circular

boolean

FALSE

是否采用衔接滑动

1.0.0

vertical

boolean

FALSE

滑动方向是否为纵向

1.0.0

previous-margin

string

"0px"

前边距,可用于露出前一项的一小部分,接受 px 和 rpx 值

1.9.0

next-margin

string

"0px"

后边距,可用于露出后一项的一小部分,接受 px 和 rpx 值

1.9.0

snap-to-edge

boolean

FALSE

当 swiper-item 的个数大于等于 2,关闭 circular 并且开启 previous-margin 或 next-margin 的时候,可以指定这个边距是否应用到第一个、最后一个元素

2.12.1

display-multiple-items

number

1

同时显示的滑块数量

1.9.0

easing-function

string

"default"

指定 swiper 切换缓动动画类型

2.6.5

合法值

说明

default

默认缓动函数

linear

线性动画

easeInCubic

缓入动画

easeOutCubic

缓出动画

easeInOutCubic

缓入缓出动画

bindchange

eventhandle

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

1.0.0

bindtransition

eventhandle

swiper-item 的位置发生改变时会触发 transition 事件,event.detail = {dx: dx, dy: dy}

2.4.3

bindanimationfinish

eventhandle

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

1.9.0

Skyline

属性

说明

previous-margin

不支持

next-margin

不支持

snap-to-edge

不支持

display-multiple-items

不支持

bindanimationfinish

暂未支持

change事件 source 返回值

从 1.4.0 开始,change事件增加 source字段,表示导致变更的原因,可能值如下:

1.autoplay 自动播放导致 swiper 变化;

2.touch 用户划动引起 swiper 变化;

3.其它原因将用空字符串表示。

Bug & Tip

1.tip: 如果在 bindchange 的事件回调函数中使用 setData 改变 current 值,则有可能导致 setData 被不停地调用,因而通常情况下请在改变 current 值前检测 source 字段来判断是否是由于用户触摸引起。

2.tip: 在 mac 小程序上,若当前组件所在的页面或全局开启了 enablePassiveEvent 配置项,该内置组件可能会出现非预期表现(详情参考 enablePassiveEvent 文档)

示例代码:

JAVASCRIPT

代码语言:javascript
复制
Page({
  onShareAppMessage() {
    return {
      title: 'swiper',
      path: 'page/component/pages/swiper/swiper'
    }
  },

  data: {
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500
  },

  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },

  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },

  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  }
})

WXML:

代码语言:javascript
复制
<view class="container">
  <view class="page-body">
    <view class="page-section page-section-spacing swiper">
      <swiper indicator-dots="{{indicatorDots}}"
        autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
        <block wx:for="{{background}}" wx:key="*this">
          <swiper-item>
            <view class="swiper-item {{item}}"></view>
          </swiper-item>
        </block>
      </swiper>
    </view>
    <view class="page-section" style="margin-top: 40rpx;margin-bottom: 0;">
      <view class="weui-cells weui-cells_after-title">
        <view class="weui-cell weui-cell_switch">
          <view class="weui-cell__bd">指示点</view>
          <view class="weui-cell__ft">
            <switch checked="{{indicatorDots}}" bindchange="changeIndicatorDots" />
          </view>
        </view>
        <view class="weui-cell weui-cell_switch">
          <view class="weui-cell__bd">自动播放</view>
          <view class="weui-cell__ft">
            <switch checked="{{autoplay}}" bindchange="changeAutoplay" />
          </view>
        </view>
      </view>
    </view>

    <view class="page-section page-section-spacing">
      <view class="page-section-title">
        <text>幻灯片切换时长(ms)</text>
        <text class="info">{{duration}}</text>
      </view>
      <slider bindchange="durationChange" value="{{duration}}" min="500" max="2000"/>
      <view class="page-section-title">
        <text>自动播放间隔时长(ms)</text>
        <text class="info">{{interval}}</text>
      </view>
      <slider bindchange="intervalChange" value="{{interval}}" min="2000" max="10000"/>
    </view>
  </view>
</view>

添加描述

版权声明: 本站所有内容均由互联网收集整理、上传,如涉及版权问题,请联系我们第一时间处理。

原文链接地址:

https://developers.weixin.qq.com/miniprogram/dev/component/swiper.html

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档