1e41f4b71Sopenharmony_ci# swiper
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci>  **说明:**
4e41f4b71Sopenharmony_ci>  从API version 4开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci滑动容器,提供切换子组件显示的能力。
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci## 权限列表
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci## 子组件
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci可以包含子组件。
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## 属性
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci除支持[通用属性](js-components-common-attributes.md)外,还支持如下属性:
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci| 名称                             | 类型      | 默认值    | 必填   | 描述                                       |
23e41f4b71Sopenharmony_ci| ------------------------------ | ------- | ------ | ---- | ---------------------------------------- |
24e41f4b71Sopenharmony_ci| index                          | number  | 0      | 否    | 当前在容器中显示的子组件的索引值。                        |
25e41f4b71Sopenharmony_ci| autoplay                       | boolean | false  | 否    | 子组件是否自动播放,自动播放状态下,导航点不可操作<sup>5+</sup>。  |
26e41f4b71Sopenharmony_ci| interval                       | number  | 3000   | 否    | 使用自动播放时播放的时间间隔,单位为ms。                    |
27e41f4b71Sopenharmony_ci| indicator                      | boolean | true   | 否    | 是否启用导航点指示器,默认true。                       |
28e41f4b71Sopenharmony_ci| digital<sup>5+</sup>           | boolean | false  | 否    | 是否启用数字导航点,默认为false。<br/>必须设置indicator时才能生效数字导航点。 |
29e41f4b71Sopenharmony_ci| indicatordisabled<sup>5+</sup> | boolean | false  | 否    | 指示器是否禁止用户手势操作,设置为true时,指示器不会响应用户的点击拖拽。   |
30e41f4b71Sopenharmony_ci| loop                           | boolean | true   | 否    | 是否开启循环滑动。                                |
31e41f4b71Sopenharmony_ci| duration                       | number  | -      | 否    | 子组件切换的动画时长。                              |
32e41f4b71Sopenharmony_ci| vertical                       | boolean | false  | 否    | 是否为纵向滑动,纵向滑动时采用纵向的指示器。                   |
33e41f4b71Sopenharmony_ci| cachedsize<sup>7+</sup>        | number  | -1     | 否    | swiper延迟加载时item最少缓存数量。-1表示全部缓存。          |
34e41f4b71Sopenharmony_ci| scrolleffect<sup>7+</sup>      | string  | spring | 否    | 滑动效果。目前支持如下:<br/>-&nbsp;spring:弹性物理动效,滑动到边缘后可以根据初始速度或通过触摸事件继续滑动一段距离,松手后回弹。<br/>-&nbsp;fade:渐隐物理动效,滑动到边缘后展示一个波浪形的渐隐,根据速度和滑动距离的变化渐隐也会发送一定的变化<br/>-&nbsp;none:滑动到边缘后无效果。<br/>该属性仅在loop属性为false时生效。 |
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci## 样式
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci除支持[通用样式](js-components-common-styles.md)外,还支持如下样式:
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci| 名称                                 | 类型                                       | 默认值        | 必填   | 描述                   |
42e41f4b71Sopenharmony_ci| ---------------------------------- | ---------------------------------------- | ---------- | ---- | -------------------- |
43e41f4b71Sopenharmony_ci| indicator-color                    | &lt;color&gt;                            | -          | 否    | 导航点指示器的填充颜色。         |
44e41f4b71Sopenharmony_ci| indicator-selected-color           | &lt;color&gt;                            | \#ff007dff | 否    | 导航点指示器选中的颜色。         |
45e41f4b71Sopenharmony_ci| indicator-size                     | &lt;length&gt;                           | 4px        | 否    | 导航点指示器的直径大小。         |
46e41f4b71Sopenharmony_ci| indicator-top\|left\|right\|bottom | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt; | -          | 否    | 导航点指示器在swiper中的相对位置。 |
47e41f4b71Sopenharmony_ci| next-margin<sup>7+</sup>           | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt; | -          | 否    | 后边距,用于露出后一项的一小部分。    |
48e41f4b71Sopenharmony_ci| previous-margin<sup>7+</sup>       | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt; | -          | 否    | 前边距,用于露出前一项的一小部分。    |
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci## 事件
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci除支持[通用事件](js-components-common-events.md)外,还支持如下事件:
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci| 名称                           | 参数                                      | 描述                 |
56e41f4b71Sopenharmony_ci| ---------------------------- | --------------------------------------- | ------------------ |
57e41f4b71Sopenharmony_ci| change                       | {&nbsp;index:&nbsp;currentIndex&nbsp;}  | 当前显示的组件索引变化时触发该事件。 |
58e41f4b71Sopenharmony_ci| rotation                     | {&nbsp;value:&nbsp;rotationValue&nbsp;} | 智能穿戴表冠旋转事件触发时的回调。  |
59e41f4b71Sopenharmony_ci| animationfinish<sup>7+</sup> | -                                       | 动画结束时触发该事件。        |
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci## 方法
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci除支持[通用方法](js-components-common-methods.md)外,还支持如下方法:
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci| 名称           | 参数                                     | 描述              |
66e41f4b71Sopenharmony_ci| ------------ | -------------------------------------- | --------------- |
67e41f4b71Sopenharmony_ci| swipeTo      | {&nbsp;index:&nbsp;number(指定位置)&nbsp;} | 切换到index位置的子组件。 |
68e41f4b71Sopenharmony_ci| showNext     | 无                                      | 显示下一个子组件。       |
69e41f4b71Sopenharmony_ci| showPrevious | 无                                      | 显示上一个子组件。       |
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci## 示例
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci```html
75e41f4b71Sopenharmony_ci<!-- xxx.hml -->
76e41f4b71Sopenharmony_ci<div class="container">
77e41f4b71Sopenharmony_ci  <swiper class="swiper" id="swiper" index="0" indicator="true" loop="true" digital="false" cachedsize="-1"
78e41f4b71Sopenharmony_ci    scrolleffect="spring">
79e41f4b71Sopenharmony_ci    <div class = "swiperContent1" >
80e41f4b71Sopenharmony_ci      <text class = "text">First screen</text>
81e41f4b71Sopenharmony_ci    </div>
82e41f4b71Sopenharmony_ci    <div class = "swiperContent2">
83e41f4b71Sopenharmony_ci      <text class = "text">Second screen</text>
84e41f4b71Sopenharmony_ci    </div>
85e41f4b71Sopenharmony_ci    <div class = "swiperContent3">
86e41f4b71Sopenharmony_ci      <text class = "text">Third screen</text>
87e41f4b71Sopenharmony_ci    </div>
88e41f4b71Sopenharmony_ci  </swiper>
89e41f4b71Sopenharmony_ci  <input class="button" type="button" value="swipeTo" onclick="swipeTo"></input>
90e41f4b71Sopenharmony_ci  <input class="button" type="button" value="showNext" onclick="showNext"></input>
91e41f4b71Sopenharmony_ci  <input class="button" type="button" value="showPrevious" onclick="showPrevious"></input>
92e41f4b71Sopenharmony_ci</div>
93e41f4b71Sopenharmony_ci```
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci```css
96e41f4b71Sopenharmony_ci/* xxx.css */
97e41f4b71Sopenharmony_ci.container {
98e41f4b71Sopenharmony_ci  flex-direction: column;
99e41f4b71Sopenharmony_ci  width: 100%;
100e41f4b71Sopenharmony_ci  height: 100%;
101e41f4b71Sopenharmony_ci  align-items: center;
102e41f4b71Sopenharmony_ci}
103e41f4b71Sopenharmony_ci.swiper {
104e41f4b71Sopenharmony_ci  flex-direction: column;
105e41f4b71Sopenharmony_ci  align-content: center;
106e41f4b71Sopenharmony_ci  align-items: center;
107e41f4b71Sopenharmony_ci  width: 70%;
108e41f4b71Sopenharmony_ci  height: 130px;
109e41f4b71Sopenharmony_ci  border: 1px solid #000000;
110e41f4b71Sopenharmony_ci  indicator-color: #cf2411;
111e41f4b71Sopenharmony_ci  indicator-size: 14px;
112e41f4b71Sopenharmony_ci  indicator-bottom: 20px;
113e41f4b71Sopenharmony_ci  indicator-right: 30px;
114e41f4b71Sopenharmony_ci  margin-top: 100px;
115e41f4b71Sopenharmony_ci  next-margin:20px;
116e41f4b71Sopenharmony_ci  previous-margin:20px;
117e41f4b71Sopenharmony_ci}
118e41f4b71Sopenharmony_ci.swiperContent1{
119e41f4b71Sopenharmony_ci  height: 100%;
120e41f4b71Sopenharmony_ci  width: 100%;
121e41f4b71Sopenharmony_ci  justify-content: center;
122e41f4b71Sopenharmony_ci  background-color: #007dff;
123e41f4b71Sopenharmony_ci}
124e41f4b71Sopenharmony_ci.swiperContent2{
125e41f4b71Sopenharmony_ci  height: 100%;
126e41f4b71Sopenharmony_ci  width: 100%;
127e41f4b71Sopenharmony_ci  justify-content: center;
128e41f4b71Sopenharmony_ci  background-color: #ff7500;
129e41f4b71Sopenharmony_ci}
130e41f4b71Sopenharmony_ci.swiperContent3{
131e41f4b71Sopenharmony_ci  height: 100%;
132e41f4b71Sopenharmony_ci  width: 100%;
133e41f4b71Sopenharmony_ci  justify-content: center;
134e41f4b71Sopenharmony_ci  background-color: #41ba41;
135e41f4b71Sopenharmony_ci}
136e41f4b71Sopenharmony_ci.button {
137e41f4b71Sopenharmony_ci  width: 70%;
138e41f4b71Sopenharmony_ci  margin: 10px;
139e41f4b71Sopenharmony_ci}
140e41f4b71Sopenharmony_ci.text {
141e41f4b71Sopenharmony_ci  font-size: 40px;
142e41f4b71Sopenharmony_ci}
143e41f4b71Sopenharmony_ci```
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci```js
146e41f4b71Sopenharmony_ci// xxx.js
147e41f4b71Sopenharmony_ciexport default {
148e41f4b71Sopenharmony_ci  swipeTo() {
149e41f4b71Sopenharmony_ci    this.$element('swiper').swipeTo({index: 2});
150e41f4b71Sopenharmony_ci  },
151e41f4b71Sopenharmony_ci  showNext() {
152e41f4b71Sopenharmony_ci    this.$element('swiper').showNext();
153e41f4b71Sopenharmony_ci  },
154e41f4b71Sopenharmony_ci  showPrevious() {
155e41f4b71Sopenharmony_ci    this.$element('swiper').showPrevious();
156e41f4b71Sopenharmony_ci  }
157e41f4b71Sopenharmony_ci}
158e41f4b71Sopenharmony_ci```
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci![zh-cn_image_0000001167823326](figures/zh-cn_image_0000001167823326.gif)
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci
163