目录

swiper

Represents a swiper view container. It can only host the swiper-item component. Otherwise, undefined behaviors may occur.

Since: 1.0.0

Constraints:

The height of the swiper component is determined by the height of its first swiper-item element.

Attributes

Name Type Default Value Mandatory Description
indicator-dots boolean false No Whether to display the indicator dots.
indicator-color string rgba(0, 0, 0, 0.3) No Indicator dot color.
indicator-active-color string #0A59F7 No Color of the selected indicator dot.
current number 0 No

Index of the current swiper.

Since: 1.0.3

autoplay boolean false No Whether to enable automatic switchover
interval number 5000 No Autoplay interval.
duration number 500 No Swiping animation duration.
circular boolean false No Whether to enable circular swiping.
vertical boolean false No Whether the swiping direction is vertical.
display-multiple-items number 1 No Number of swipers that can be displayed at the same time.
previous-margin string "0px" No Front margin, which can be used to expose a small part of the previous item. The value can be in px or rpx.
next-margin string "0px" No Back margin, which can be used to expose a small part of the next item. The value can be in px or rpx.
disable-touch boolean false No Whether to disable the touch operation.

Events

Name Parameter Description
bindchange event Event triggered when the value of current changes.
bindtransition event Event triggered when the position of swiper-item changes.
bindanimationfinish event Event triggered when the animation ends.

Example:

HXML file:

<view class="container">
  <scroll-view class="myScroll" scroll-y="true" scroll-x="true">
    <view class="page-body">
      <view class="page-section">
        <view style="padding: 0 40rpx" class="page-section-title">Dynamic setting</view>
        <swiper
          indicator-dots="{{indicatorDots}}"
          indicator-color="{{indicatorColor}}"
          indicator-active-color="{{indicatorActiveColor}}"
          current="{{current}}"
          autoplay="{{autoplay}}"
          interval="{{interval}}"
          duration="{{duration}}"
          circular="{{circular}}"
          vertical="{{vertical}}"
          display-multiple-items="{{displayMultipleItems}}"
          previous-margin="{{previousMargin}}"
          next-margin="{{nextMargin}}"
          disable-touch="{{disableTouch}}"
          bindchange="changeCurrentEvent"
          bindtransition="transitionEvent"
          bindanimationfinish="animationFinishEvent">
          <block has:for="{{background}}" has:key="*this">
            <swiper-item>
              <view class="swiper-item">Event triggered when a user drags a swiper.</view>
            </swiper-item>
          </block>
        </swiper>
        <view class="page-section-setting">
          <view class="page-section-title">set indicator-dots: whether to display the indicator dots.</view>
          <switch checked="{{indicatorDots}}" bindchange="setIndicatorDots"></switch>
          <button type="primary" size="mini" bindtap="setIndicatorDotsABC">setIndicatorDots-"abc"</button>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set autoplay: whether to enable automatic switchover.</view>
          <switch checked="{{autoplay}}" bindchange="setAutoplay"></switch>
          <button type="primary" size="mini" bindtap="setAutoplay01">"abc"</button>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set circular: whether to enable circular swiping.</view>
          <switch checked="{{circular}}" bindchange="setCircular"></switch>
          <button type="primary" size="mini" bindtap="setCircularABC">setCircular-"abc"</button>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set vertical: whether the swiping direction is vertical.</view>
          <switch checked="{{vertical}}" bindchange="setVertical"></switch>
          <button type="primary" size="mini" bindtap="setVerticalABC">setVertical-"abc"</button>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set disable-touch: whether to disable the touch operation.</view>
          <switch checked="{{disableTouch}}" bindchange="setDisableTouch"></switch>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set current: index of the current swiper.</view>
          <view class="weui-cell weui-cell_input">
            <input type="text" value="{{current}}" bindblur="setCurrent"/>
          </view>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set interval: autoplay interval.</view>
          <view class="weui-cell weui-cell_input">
            <input class="weui-input" type="text" value="{{interval}}" bindblur="setInterval"/>
          </view>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set duration: swiping animation duration.</view>
          <view class="weui-cell weui-cell_input">
            <input type="text" value="{{duration}}" bindblur="setDuration"/>
          </view>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set display-multiple-items: number of swipers that can be displayed at the same time.</view>
          <view class="weui-cell weui-cell_input">
            <input type="text" value="{{displayMultipleItems}}" bindblur="setDisplayMultipleItems"/>
          </view>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set indicator-color: indicator dot color.</view>
          <view class="weui-cell weui-cell_input">
            <input type="text" value="{{indicatorColor}}" bindblur="setIndicatorColor"/>
          </view>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set indicator-active-color: color of the selected indicator dot.</view>
          <view class="weui-cell weui-cell_input">
            <input type="text" value="{{indicatorActiveColor}}" bindblur="setIndicatorActiveColor"/>
          </view>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set previous-margin: front margin, which can be used to expose a small part of the previous item.</view>
          <view class="weui-cell weui-cell_input">
            <input type="text" value="{{previousMargin}}" bindblur="setPreviousMargin"/>
          </view>
        </view>
        <view class="page-section-setting">
          <view class="page-section-title">set next-margin: back margin, which can be used to expose a small part of the next item.</view>
          <view class="weui-cell weui-cell_input">
            <input type="text" value="{{nextMargin}}" bindblur="setNextMargin"/>
          </view>
        </view>
      </view>
    </view>
  </scroll-view>
</view>

JavaScript file:

const dataNormalize = (str) => {
  let json;
  try {
    json = JSON.parse(str);
  } catch {
    console.error('dataNormalize error');
  }
  const baseObject = {
    [str]: json ? json : str,
    'object': {},
    'null': null,
    'undefined': undefined
  };
  console.info('dataNormalize', baseObject[str]);
  return baseObject[str];
};

Page({
  data: {
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3', 'demo-text-4', 'demo-text-5', 'demo-text-6'],
    indicatorDots: true,
    autoplay: false,
    circular: false,
    vertical: false,
    disableTouch: false,
    current: 0,
    interval: 5000,
    duration: 500,
    displayMultipleItems: 1,
    indicatorColor: 'rgba(0, 0, 0, .3)',
    indicatorActiveColor: '#0A59F7',
    previousMargin: '0px',
    nextMargin: '0px'
  },
  setIndicatorDots(event) {
    console.info('setIndicatorDots', event.detail.value);
    this.setData({
      indicatorDots: event.detail.value
    });
  },
  setIndicatorDotsABC(e) {
    console.info('setIndicatorDots', 'abc');
    this.setData({
      indicatorDots: 'abc'
    });
  },
  setAutoplay(event) {
    console.info('setAutoplay', event.detail.value);
    this.setData({
      autoplay: event.detail.value
    });
  },
  setAutoplay01() {
    console.info('setAutoplay: abc');
    this.setData({
      autoplay: 'abc'
    });
  },
  setCircular(event) {
    console.info('setCircular', event.detail.value);
    this.setData({
      circular: event.detail.value
    });
  },
  setCircularABC(event) {
    console.info('setCircular', 'abc');
    this.setData({
      circular: 'abc'
    });
  },
  setVertical(event) {
    console.info('setVertical', event.detail.value);
    this.setData({
      vertical: event.detail.value
    });
  },
  setVerticalABC(event) {
    console.info('setVertical', 'abc');
    this.setData({
      vertical: 'abc'
    });
  },
  setDisableTouch(event) {
    console.info('setDisableTouch', event.detail.value);
    this.setData({
      disableTouch: event.detail.value
    });
  },
  setCurrent(event) {
    console.info('setCurrent', event.detail.value);
    this.setData({
      current: dataNormalize(event.detail.value)
    });
  },
  setInterval(event) {
    console.info('setInterval', event.detail.value);
    this.setData({
      interval: dataNormalize(event.detail.value)
    });
  },
  setDuration(event) {
    console.info('setDuration', event.detail.value);
    this.setData({
      duration: dataNormalize(event.detail.value)
    });
  },
  setDisplayMultipleItems(event) {
    console.info('setDisplayMultipleItems', event.detail.value);
    this.setData({
      displayMultipleItems: dataNormalize(event.detail.value)
    });
  },
  setIndicatorColor(event) {
    console.info('setIndicatorColor', event.detail.value);
    this.setData({
      indicatorColor: dataNormalize(event.detail.value)
    });
  },
  setIndicatorActiveColor(event) {
    console.info('setIndicatorActiveColor', event.detail.value);
    this.setData({
      indicatorActiveColor: dataNormalize(event.detail.value)
    });
  },
  setPreviousMargin(event) {
    console.info('setPreviousMargin', event.detail.value);
    this.setData({
      previousMargin: dataNormalize(event.detail.value)
    });
  },
  setNextMargin(event) {
    console.info('setNextMargin', event.detail.value);
    this.setData({
      nextMargin: dataNormalize(event.detail.value)
    });
  },
  changeCurrentEvent(event) {
    // Example of event.detail: {"current":0,"currentItemId":"","source":""}
    console.info('A changeCurrent event occurred in the swiper component. Carried data: ', event.detail)
  },
  transitionEvent(event) {
    // Example of event.detail: {"dx":0,"dy":0}
    console.info('A transition event occurred in the swiper component. Carried data: ', event.detail)
  },
  animationFinishEvent(event) {
    // Example of event.detail: {"current":0,"currentItemId":"","source":""}
    console.info('An animationFinish event occurred in the swiper component. Carried data: ', event.detail)
  }
});

CSS file:

.page-section {
  width: 100%;
  margin-bottom: 60rpx;
}

.page-section-setting {
  border-radius: 40rpx;
  border: 1px solid #ccc;
  margin: 20rpx 10rpx;
  padding: 20rpx 0;
}

.page-section-title {
  font-size: 32rpx;
  margin-bottom: 10rpx;
  padding: 20rpx;
  font-weight: 800;
  color: #0A59F7;
}

button, switch {
  margin-left: 40rpx;
}

.weui-cell_input {
  background-color: rgba(0, 0, 0, 0.047);
  color: #0A59F7;
  margin: 0 40rpx;
}

.swiper-item {
  background-color: rgba(0, 0, 0, 0.047);
  margin: 0 40rpx;
  display: block;
  height: 100%;
}