ts-container-swiper.md 8.0 KB
Newer Older
Z
zengyawen 已提交
1
# Swiper
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
 The **\<Swiper>** component is able to display child components in looping mode.
E
ester.zhou 已提交
4 5

> **NOTE**
E
ester.zhou 已提交
6
>
Z
zengyawen 已提交
7
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10

## Child Components
Z
zengyawen 已提交
11 12 13

This component can contain child components.

Z
zengyawen 已提交
14 15 16

## APIs

E
ester.zhou 已提交
17
Swiper(controller?: SwiperController)
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19
**Parameters**
Z
zengyawen 已提交
20

E
ester.zhou 已提交
21 22
| Name       | Type                          | Mandatory | Description                |
| ---------- | ------------------------------------- | ---- | -------------------- |
E
ester.zhou 已提交
23
| controller | [SwiperController](#swipercontroller) | No  | Controller bound to the component to control the page switching.|
Z
zengyawen 已提交
24 25


E
ester.zhou 已提交
26
## Attributes
Z
zengyawen 已提交
27

E
ester.zhou 已提交
28
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported. [Menu control](ts-universal-attributes-menu.md) is not supported.
E
ester.zhou 已提交
29 30 31 32

| Name                         | Type                              | Description                                      |
| --------------------------- | ---------------------------------------- | ---------------------------------------- |
| index                       | number                                   | Index of the child component currently displayed in the container.<br>Default value: **0**                    |
E
ester.zhou 已提交
33
| autoPlay                    | boolean                                  | Whether to enable automatic playback for child component switching.<br>Default value: **false**   |
E
ester.zhou 已提交
34 35 36 37 38
| interval                    | number                                   | Interval for automatic playback, in ms.<br>Default value: **3000**                   |
| indicator                   | boolean                                  | Whether to enable the navigation dots indicator.<br>Default value: **true**                             |
| loop                        | boolean                                  | Whether to enable loop playback.<br>The value **true** means to enable loop playback. When LazyForEach is used, it is recommended that the number of the components to load exceed 5.<br>Default value: **true**|
| duration                    | number                                   | Duration of the animation for switching child components, in ms.<br>Default value: **400**                       |
| vertical                    | boolean                                  | Whether vertical swiping is used.<br>Default value: **false**                                |
E
ester.zhou 已提交
39
| itemSpace                   | number \| string          | Space between child components.<br>Default value: **0**                          |
E
ester.zhou 已提交
40 41 42
| displayMode                 | SwiperDisplayMode                        | Mode in which elements are displayed along the main axis. This attribute takes effect only when **displayCount** is not set.<br>Default value: **SwiperDisplayMode.Stretch**|
| cachedCount<sup>8+</sup>    | number                                   | Number of child components to be cached.<br>Default value: **1**                             |
| disableSwipe<sup>8+</sup>   | boolean                                  | Whether to disable the swipe feature.<br>Default value: **false**                             |
E
esterzhou 已提交
43 44 45
| curve<sup>8+</sup>          | [Curve](ts-appendix-enums.md#curve)  \| string | Animation curve. The ease-in/ease-out curve is used by default. For details about common curves, see [Curve](ts-appendix-enums.md#curve). You can also create custom curves (interpolation curve objects) by using the API provided by the [interpolation calculation](../apis/js-apis-curve.md) module.<br>Default value: **Curve.Ease**|
| indicatorStyle<sup>8+</sup> | {<br>left?: [Length](ts-types.md#length),<br>top?: [Length](ts-types.md#length),<br>right?: [Length](ts-types.md#length),<br>bottom?: [Length](ts-types.md#length),<br>size?: [Length](ts-types.md#length),<br>mask?: boolean,<br>color?: [ResourceColor](ts-types.md),<br>selectedColor?: [ResourceColor](ts-types.md)<br>} | Style of the navigation dots indicator.<br>\- **left**: distance between the navigation dots indicator and the left edge of the **\<Swiper>** component.<br>\- **top**: distance between the navigation dots indicator and the top edge of the **\<Swiper>** component.<br>\- **right**: distance between the navigation dots indicator and the right edge of the **\<Swiper>** component.<br>\- **bottom**: distance between the navigation dots indicator and the bottom edge of the **\<Swiper>** component.<br>\- **size**: diameter of the navigation dots indicator.<br>\- **mask**: whether to enable the mask for the navigation dots indicator.<br>\- **color**: color of the navigation dots indicator.<br>\- **selectedColor**: color of the selected navigation dot.|
| displayCount<sup>8+</sup>   | number\|string                                               | Number of elements to display per page.<br>Default value: **1**                                          |
E
ester.zhou 已提交
46
| effectMode<sup>8+</sup>     | [EdgeEffect](ts-appendix-enums.md#edgeeffect)  | Swipe effect. For details, see **EdgeEffect**.<br>Default value: **EdgeEffect.Spring**|
E
ester.zhou 已提交
47 48 49 50 51 52

## SwiperDisplayMode

| Name| Description|
| ----------- | ------------------------------------------ |
| Stretch     | The slide width of the **\<Swiper>** component is equal to the width of the component.|
E
ester.zhou 已提交
53
| AutoLinear  | The slide width of the **\<Swiper>** component is equal to that of the child component with the maximum width.|
E
ester.zhou 已提交
54

E
ester.zhou 已提交
55
## SwiperController
Z
zengyawen 已提交
56

E
ester.zhou 已提交
57
Controller of the **\<Swiper>** component. You can bind this object to the **\<Swiper>** component and use it to control page switching.
E
ester.zhou 已提交
58 59 60 61 62 63 64 65 66 67

### showNext

showNext(): void

Turns to the next page.

### showPrevious

showPrevious(): void
Z
zengyawen 已提交
68

E
ester.zhou 已提交
69
Turns to the previous page.
Z
zengyawen 已提交
70

E
ester.zhou 已提交
71 72 73 74
### finishAnimation

finishAnimation(callback?: () => void): void

E
ester.zhou 已提交
75
Stops an animation.
E
ester.zhou 已提交
76 77 78

**Parameters**

E
ester.zhou 已提交
79
| Name   | Type  | Mandatory | Description|
E
ester.zhou 已提交
80
| --------- | ---------- | ------ | -------- |
E
esterzhou 已提交
81
| callback  | () => void | No    | Callback invoked when the animation stops.|
Z
zengyawen 已提交
82 83 84

## Events

E
ester.zhou 已提交
85
### onChange
Z
zengyawen 已提交
86

E
ester.zhou 已提交
87
onChange(event: (index: number) => void)
Z
zengyawen 已提交
88

E
ester.zhou 已提交
89
Triggered when the index of the currently displayed child component changes.
Z
zengyawen 已提交
90

E
ester.zhou 已提交
91 92 93 94
> **NOTE**
> 
> When the **\<Swiper>** component is used together with **LazyForEach**, the subpage UI cannot be refreshed in the **onChange** event.

E
esterzhou 已提交
95
**Return value**
E
ester.zhou 已提交
96

E
esterzhou 已提交
97 98 99
| Name   |   Type   | Description|
| --------- | ---------- | -------- |
| index     | number    | Index of the currently displayed element.|
E
ester.zhou 已提交
100 101 102


## Example
Z
zengyawen 已提交
103

E
ester.zhou 已提交
104 105
```ts
// xxx.ets
Z
zengyawen 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
class MyDataSource implements IDataSource {
  private list: number[] = []
  private listener: DataChangeListener

  constructor(list: number[]) {
    this.list = list
  }

  totalCount(): number {
    return this.list.length
  }

  getData(index: number): any {
    return this.list[index]
  }

  registerDataChangeListener(listener: DataChangeListener): void {
    this.listener = listener
  }

  unregisterDataChangeListener() {
  }
}

Z
zengyawen 已提交
130 131 132 133
@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
Z
zengyawen 已提交
134 135
  private data: MyDataSource = new MyDataSource([])

E
ester.zhou 已提交
136
  aboutToAppear(): void {
Z
zengyawen 已提交
137 138 139 140 141 142
    let list = []
    for (var i = 1; i <= 10; i++) {
      list.push(i.toString());
    }
    this.data = new MyDataSource(list)
  }
Z
zengyawen 已提交
143 144 145 146

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
Z
zengyawen 已提交
147
        LazyForEach(this.data, (item: string) => {
E
esterzhou 已提交
148
          Text(item).width('90%').height(160).backgroundColor(0xAFEEEE).textAlign(TextAlign.Center).fontSize(30)
Z
zengyawen 已提交
149
        }, item => item)
Z
zengyawen 已提交
150
      }
Z
zengyawen 已提交
151
      .cachedCount(2)
Z
zengyawen 已提交
152 153 154
      .index(1)
      .autoPlay(true)
      .interval(4000)
E
esterzhou 已提交
155 156
      .indicator(true)
      .loop(true)
Z
zengyawen 已提交
157 158
      .duration(1000)
      .itemSpace(0)
E
esterzhou 已提交
159
      .curve(Curve.Linear)
Z
zengyawen 已提交
160 161 162 163
      .onChange((index: number) => {
        console.info(index.toString())
      })

E
esterzhou 已提交
164 165
      Row({ space: 12 }) {
        Button('showNext')
Z
zengyawen 已提交
166 167 168
          .onClick(() => {
            this.swiperController.showNext()
          })
E
esterzhou 已提交
169
        Button('showPrevious')
Z
zengyawen 已提交
170 171 172
          .onClick(() => {
            this.swiperController.showPrevious()
          })
E
esterzhou 已提交
173 174 175
      }.margin(5)
    }.width('100%')
    .margin({ top: 5 })
Z
zengyawen 已提交
176 177 178 179
  }
}
```

E
esterzhou 已提交
180
![swiper](figures/swiper.gif)