ts-container-swiper.md 8.9 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 33 34 35 36 37 38

| Name                         | Type                              | Description                                      |
| --------------------------- | ---------------------------------------- | ---------------------------------------- |
| index                       | number                                   | Index of the child component currently displayed in the container.<br>Default value: **0**                    |
| autoPlay                    | boolean                                  | Whether to enable automatic playback for child component switching. If this attribute is **true**, the navigation dots indicator does not take effect.<br>Default value: **false**   |
| 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
ester.zhou 已提交
43 44
| 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](ts-interpolation-calculation.md)) by using the API provided by the interpolation calculation 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#resourcecolor),<br>selectedColor?: [ResourceColor](ts-types.md#resourcecolor)<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.|
E
ester.zhou 已提交
45
| displayCount<sup>8+</sup>   | number\|string                                               | Number of elements to display.<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 55 56 57 58

## EdgeEffect

| Name  | Description                                                                     |
| ------ | ------------------------------------------------------------------------- |
E
ester.zhou 已提交
59 60 61
| Spring | Spring effect. When sliding to one edge, the component can move beyond the bounds through touches, and produce a bounce effect when the user releases their finger.   |
| Fade   | Fade effect. When sliding to one edge, the component can move beyond the bounds through touches, and produce a fade effect along the way; when the user releases their finger, the fade changes.           |
| None   | No effect. When sliding to one edge, the component cannot move beyond the bounds.                                                     |
Z
zengyawen 已提交
62

E
ester.zhou 已提交
63
## SwiperController
Z
zengyawen 已提交
64

65
Controller of the **\<Swiper>** component. You can bind this object to the **\<Swiper>** component and use it to control page switching.
E
ester.zhou 已提交
66 67 68 69 70 71 72 73 74 75

### showNext

showNext(): void

Turns to the next page.

### showPrevious

showPrevious(): void
Z
zengyawen 已提交
76

E
ester.zhou 已提交
77
Turns to the previous page.
Z
zengyawen 已提交
78

E
ester.zhou 已提交
79 80 81 82
### finishAnimation

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

E
ester.zhou 已提交
83
Stops an animation.
E
ester.zhou 已提交
84 85 86 87 88 89

**Parameters**

| Name   | Type  | Mandatory.| Description|
| --------- | ---------- | ------ | -------- |
| callback  | () => void | Yes    | Callback invoked when the animation stops.|
Z
zengyawen 已提交
90 91 92

## Events

E
ester.zhou 已提交
93
### onChange
Z
zengyawen 已提交
94

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

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

E
ester.zhou 已提交
99 100 101 102 103 104 105 106
**Parameters**

| Name   | Type  | Mandatory.| Description|
| --------- | ---------- | ------ | -------- |
| index     | number     | Yes    | Index of the currently displayed element.|


## Example
Z
zengyawen 已提交
107

E
ester.zhou 已提交
108 109
```ts
// xxx.ets
Z
zengyawen 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
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 已提交
134 135 136 137
@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
Z
zengyawen 已提交
138 139
  private data: MyDataSource = new MyDataSource([])

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

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
Z
zengyawen 已提交
151 152 153
        LazyForEach(this.data, (item: string) => {
          Text(item).width('90%').height(160).backgroundColor(0xAFEEEE).textAlign(TextAlign.Center).fontSize(20)
        }, item => item)
Z
zengyawen 已提交
154
      }
Z
zengyawen 已提交
155
      .cachedCount(2)
Z
zengyawen 已提交
156 157 158 159 160 161 162 163
      .index(1)
      .autoPlay(true)
      .interval(4000)
      .indicator(true) // Navigation dots are enabled by default.
      .loop(false) // Loop playback is enabled by default.
      .duration(1000)
      .vertical(false) // Horizontal swiping is enabled by default.
      .itemSpace(0)
E
ester.zhou 已提交
164
      .curve(Curve.Linear) // Animation curve.
Z
zengyawen 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      .onChange((index: number) => {
        console.info(index.toString())
      })

      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('next')
          .onClick(() => {
            this.swiperController.showNext()
          })
        Button('preview')
          .onClick(() => {
            this.swiperController.showPrevious()
          })
      }
    }.margin({ top: 5 })
  }
}
```

Z
zengyawen 已提交
184
![en-us_image_0000001211898474](figures/en-us_image_0000001211898474.gif)