文档管理中心
我的
API参考快应用组件swiper

swiper

概述

滑块视图容器。

使用限制

展开

限制条件

说明

适用终端

手机、平板、智慧屏、车机

适用区域

全球

子组件

支持除<list>之外的子组件。

注意

swiper子组件不支持position:fixed,1116版本开始,swiper子组件设置position:fixed会自动转换成position:relative

属性

除了支持 通用属性以外,还支持如下属性。

展开

名称

类型

默认值

是否必填

描述

index

number

0

否

当前显示的子组件索引。

autoplay

boolean

false

否

渲染完成后,是否自动进行播放。

interval

number

3000

否

自动播放时的时间间隔,单位毫秒。

indicator

boolean

true

否

是否启用indicator,默认true。

loop

boolean

true

否

是否开启循环模式。

duration(1040+)

number

-

否

滑动动画时长。

vertical(1040+)

boolean

false

否

滑动方向是否为纵向,纵向时 indicator 也为纵向。

previousmargin(1072+)

string

0px

否

前边距,用于露出前一项的一小部分,支持单位:px和%。

nextmargin(1072+)

string

0px

否

后边距,用于露出后一项的一小部分,支持单位:px和%。

enableswipe(1080+)

boolean

true

否

是否支持swiper沿其内容的排列方向滑动。

说明

previousmargin和nextmargin的总和不应该超过整个swiper大小的1/2,超过部分将会被截取。

样式

除了支持 通用样式 以外,还支持如下样式。

说明

swiper组件的直接子节点不支持修改宽度和高度,宽度默认和swiper宽度相等。

展开

名称

类型

默认值

是否必填

描述

indicator-color

color

rgba(0,0,0,0.50)

否

indicator填充颜色

indicator-selected-color

color

rgba(51,180,255,1.0)

否

indicator选中时的颜色

indicator-size

length

20px

否

indicator组件的直径大小

indicator-top|left|right|bottom(1040+)

length | percentage

-

否

indicator相对于swiper的位置

除了支持 动画样式 以外,还支持页面切换动画。

说明

swiper动画在设置previousmargin和nextmargin场景下不生效。

展开

名称

类型

默认值

描述

page-transform-origin(1080+)

position

0px 0px

变换原点位置。取值中两个数值分别对应X轴和Y轴的坐标,单位为px,例如150px 200px 。

transform(1080+)

string

-

详情请参见“transform操作” 。

page-animation-name(1080+)

string

-

对应@keyframes的name取值,详情请参见 “@keyframes属性”。

animation-timing-function(1080+)

linear | ease | ease-in | ease-out | ease-in-out

ease

CSS动画在每个周期中的速度曲线。

transform操作

展开

方法

类型

translate

length

translateX

length

translateY

length

scale

multiple

scaleX

multiple

scaleY

multiple

rotate

deg | rad

rotateX

deg | rad

rotateY

deg | rad

@keyframes属性

展开

名称

类型

默认值

描述

background-color

color

-

-

opacity

number

-

-

transform

string

-

仅translateX和translateY支持百分比。

使用from和to来标识起点和终点,示例代码如下:

收起
自动换行
深色代码主题
复制
  1. .sampleAnimation {
  2. page-animation-name: Opacity, Rotate;
  3. page-transform-origin: 200px 200px;
  4. animation-timing-function: ease-in-out;
  5. }
  6. @keyframes Opacity {
  7. from {
  8. opacity: 0.2;
  9. }
  10. to {
  11. opacity: 1;
  12. }
  13. }
  14. @keyframes Rotate {
  15. from {
  16. transform: rotate(0deg);
  17. }
  18. to {
  19. transform: rotate(180deg);
  20. }
  21. }

事件

除了支持 通用事件 以外,还支持如下事件。

展开

名称

参数

描述

change

{index:currentIndex}

当前显示的组件索引变化时触发。

方法

展开

名称

参数

描述

swipeTo

{index: number(指定位置)}

swiper滚动到index位置。

示例如下:

收起
自动换行
深色代码主题
复制
  1. this.$element('swiper').swipeTo({ index:2 })

示例代码

收起
自动换行
深色代码主题
复制
  1. <template>
  2. <div class="container">
  3. <div class="page-title-wrap">
  4. <text class="page-title">{{componentName}}</text>
  5. </div>
  6. <div class="item-container">
  7. <div class="swiper-container">
  8. <swiper class="swiper pageAnimation" vertical="{{vertical}}" id="swiper" autoplay="{{autoPlay}}" enableswipe="{{enableswipe}}" interval="{{sliderValue}}"
  9. indicator="{{indicator}}" loop="{{isLoop}}" onchange="change" @click="swiperClick" duration="{{sliderValueForDuration}}"
  10. previousmargin="50px" nextmargin="50px" style="height:{{heightSwiper}};;indicator-top:70px ;indicator-left:{{MarginLeft}};indicator-size:{{iSize}}">
  11. <text class="item color-1">A</text>
  12. <text class="item color-2">B</text>
  13. <text class="item color-3">C</text>
  14. <text class="item color-4">D</text>
  15. <text class="item color-5">E</text>
  16. <text class="item color-6">F</text>
  17. </swiper>
  18. </div>
  19. </div>
  20. <div class="item-container">
  21. <div>
  22. <text class="item-content_title">指示点</text>
  23. <switch class="switch" checked="true" onchange="showIndicator"></switch>
  24. </div>
  25. <div>
  26. <text class="item-content_title">自动播放</text>
  27. <switch class="switch" onchange="startAutoPlay"></switch>
  28. </div>
  29. <div>
  30. <text class="item-content_title">能否循环滚动</text>
  31. <switch class="switch" checked="true" onchange="onLoopChanged"></switch>
  32. </div>
  33. <div>
  34. <text class="item-content_title">竖向</text>
  35. <switch class="switch" checked="true" onchange="onVerticalChanged"></switch>
  36. </div>
  37. </div>
  38. <div class="item-container">
  39. <div>
  40. <text class="item-content_title">自动播放间隔时长(ms)</text>
  41. <text class="item_left">{{sliderValue}}</text>
  42. </div>
  43. <slider class="slider" min="1000" max="3000" onchange="setSliderValue"></slider>
  44. </div>
  45. <div class="item-container">
  46. <div>
  47. <text class="item-content_title">动画时长(ms)</text>
  48. <text class="item_left">{{sliderValueForDuration}}</text>
  49. </div>
  50. <slider class="slider" min="0" max="1000" onchange="sliderValueForDuration"></slider>
  51. </div>
  52. <div class="item-container">
  53. <div>
  54. <text class="item-content_title">MarginLeft(px)</text>
  55. <text class="item_left">{{MarginLeft}}</text>
  56. </div>
  57. <slider class="slider" min="0" max="1000" onchange="setMarginLeft"></slider>
  58. </div>
  59. <div class="item-container">
  60. <div>
  61. <text class="item-content_title">heightSwiper(px)</text>
  62. <text class="item_left">{{heightSwiper}}</text>
  63. </div>
  64. <slider class="slider" min="250" max="1000" onchange="setHeightSwiper"></slider>
  65. </div>
  66. <div>
  67. <input type="button" class="btn" value="来到第二页" onclick="goIndex" />
  68. </div>
  69. </div>
  70. </template>
  71. <style>
  72. @import '../../../Common/css/common.css';
  73. .item-container {
  74. margin-bottom: 50px;
  75. margin-right: 60px;
  76. margin-left: 60px;
  77. flex-direction: column;
  78. }
  79. .swiper-container {
  80. flex-direction: column;
  81. }
  82. .swiper {
  83. flex-direction: column;
  84. indicator-color: #800080;
  85. indicator-selected-color: #000000;
  86. height: 250px;
  87. }
  88. .item-content_title {
  89. flex: 1;
  90. padding-top: 20px;
  91. padding-bottom: 20px;
  92. }
  93. .item_left {
  94. padding: 20px;
  95. }
  96. .switch {
  97. height: 50px;
  98. width: 320px;
  99. }
  100. .slider {
  101. width: 100%;
  102. padding-left: 50px;
  103. padding-right: 50px;
  104. }
  105. .item {
  106. height: 250px;
  107. text-align: center;
  108. color: #ffffff;
  109. }
  110. .color-1 {
  111. background-color: #09ba07;
  112. }
  113. .color-2 {
  114. background-color: #f76160;
  115. }
  116. .color-3 {
  117. background-color: #0faeff;
  118. }
  119. .color-4 {
  120. background-color: #9acd32;
  121. }
  122. .color-5 {
  123. background-color: #000000;
  124. }
  125. .color-6 {
  126. background-color: #ffff00;
  127. }
  128. .pageAnimation {
  129. page-animation-name: scaleX;
  130. animation-timing-function: linear;
  131. }
  132. @keyframes scaleX {
  133. from {
  134. transform: scaleX(0);
  135. }
  136. to {
  137. transform: scaleX(1);
  138. }
  139. }
  140. </style>
  141. <script>
  142. import prompt from '@system.prompt'
  143. export default {
  144. data: {
  145. heightSwiper: '250px',
  146. componentName: 'swiper',
  147. autoPlay: false,
  148. enableswipe:true,
  149. indicator: true,
  150. sliderValue: 1000,
  151. sliderValueForDuration: 300,
  152. isLoop: true,
  153. vertical: true,
  154. MarginLeft: '0px',
  155. iSize: '20px'
  156. },
  157. onInit() {
  158. this.$page.setTitleBar({ text: 'swiper' })
  159. },
  160. startAutoPlay: function (e) {
  161. this.autoPlay = e.checked
  162. },
  163. showIndicator: function (e) {
  164. this.indicator = e.checked
  165. },
  166. setSliderValue: function (e) {
  167. this.sliderValue = e.progress
  168. },
  169. onLoopChanged: function (e) {
  170. this.isLoop = e.checked
  171. },
  172. goIndex: function () {
  173. this.$element('swiper').swipeTo({ index: 2 })
  174. },
  175. change: function (e) {
  176. prompt.showToast({
  177. message: `播放中。。第 ${e.index} 页`
  178. })
  179. },
  180. swiperClick: function (e) {
  181. prompt.showToast({
  182. message: 'Swiper click'
  183. })
  184. },
  185. onVerticalChanged: function (e) {
  186. this.vertical = e.checked
  187. },
  188. sliderValueForDuration: function (e) {
  189. this.sliderValueForDuration = e.progress
  190. },
  191. setMarginLeft: function (e) {
  192. this.MarginLeft = e.progress + 'px'
  193. },
  194. setHeightSwiper: function (e) {
  195. this.heightSwiper = e.progress + 'px'
  196. }
  197. }
  198. </script>

效果图如下:

版本更新说明

展开

版本

发布日期

描述

1080

2021-12-28

  • 新增enableswipe属性。
  • 新增页面切换动画。

1050

2019-09-20

  • 新增 duration(设置滑动动画时长)、vertical(是否纵向滑动)属性。
  • 新增top | left | right | bottom样式,可用于设置indicator相对于swiper的位置。

1010

2018-04-20

新增loop属性,用于控制是否开启循环模式,默认值为true。

相关链接