1# Creating a Swiper 2 3 4The \<[Swiper](../reference/arkui-ts/ts-container-swiper.md)> component is a container that is able to display child components in looping mode. It is typically used in scenarios such as display of recommended content on the home page. 5 6 7## Layout and Constraints 8 9The size of the **\<Swiper>** component follows its own size settings (if configured) or adapts based on the size of its child components. 10 11 12## Loop Playback 13 14The **loop** attribute sets whether to enable loop playback. Its default value is **true**. 15 16When **loop** is set to **true**, the user can switch to the previous or next page when they are on the first or last page. 17 18 Example of setting **loop** to **true**: 19 20```ts 21... 22private swiperController: SwiperController = new SwiperController() 23... 24Swiper(this.swiperController) { 25 Text("0") 26 .width('90%') 27 .height('100%') 28 .backgroundColor(Color.Gray) 29 .textAlign(TextAlign.Center) 30 .fontSize(30) 31 32 Text("1") 33 .width('90%') 34 .height('100%') 35 .backgroundColor(Color.Green) 36 .textAlign(TextAlign.Center) 37 .fontSize(30) 38 39 Text("2") 40 .width('90%') 41 .height('100%') 42 .backgroundColor(Color.Blue) 43 .textAlign(TextAlign.Center) 44 .fontSize(30) 45} 46.loop(true) 47``` 48 49![loop_true](figures/loop_true.gif) 50 51 Example of setting **loop** to **false**: 52 53```ts 54Swiper(this.swiperController) { 55 Text("0") 56 .width('90%') 57 .height('100%') 58 .backgroundColor(Color.Gray) 59 .textAlign(TextAlign.Center) 60 .fontSize(30) 61 62 Text("1") 63 .width('90%') 64 .height('100%') 65 .backgroundColor(Color.Green) 66 .textAlign(TextAlign.Center) 67 .fontSize(30) 68 69 Text("2") 70 .width('90%') 71 .height('100%') 72 .backgroundColor(Color.Blue) 73 .textAlign(TextAlign.Center) 74 .fontSize(30) 75} 76.loop(false) 77``` 78 79![loop_false](figures/loop_false.gif) 80 81 82## Automatic Playback 83 84The **autoPlay** attribute sets whether to enable automatic playback for child component switching. Its default value is **false**. 85 86When **autoPlay** is set to **true**, automatic playback is enabled for child component switching. The playback interval is specified by the **interval** attribute, which is **3000** by default, in milliseconds. 87 88 Example of setting **autoPlay** to **true**: 89 90```ts 91Swiper(this.swiperController) { 92 Text("0") 93 .width('90%') 94 .height('100%') 95 .backgroundColor(Color.Gray) 96 .textAlign(TextAlign.Center) 97 .fontSize(30) 98 99 Text("1") 100 .width('90%') 101 .height('100%') 102 .backgroundColor(Color.Green) 103 .textAlign(TextAlign.Center) 104 .fontSize(30) 105 106 Text("2") 107 .width('90%') 108 .height('100%') 109 .backgroundColor(Color.Pink) 110 .textAlign(TextAlign.Center) 111 .fontSize(30) 112} 113.loop(true) 114.autoPlay(true) 115.interval(1000) 116``` 117 118![autoPlay](figures/autoPlay.gif) 119 120 121## Navigation Dots Indicator 122 123The **\<Swiper>** component provides a navigation dots indicator, which is displayed in the bottom center of the component. You can customize the position and style of the navigation dots indicator through the **indicatorStyle **attribute. 124 125With the **indicatorStyle** attribute, you can set the position of the navigation dots indicator relative to the edges of the **\<Swiper>** component, in addition to the size, color, and mask of each navigation dot as well as the color of the selected navigation dot. 126 127 Example of using the navigation dots indicator in its default style: 128 129```ts 130Swiper(this.swiperController) { 131 Text("0") 132 .width('90%') 133 .height('100%') 134 .backgroundColor(Color.Gray) 135 .textAlign(TextAlign.Center) 136 .fontSize(30) 137 138 Text("1") 139 .width('90%') 140 .height('100%') 141 .backgroundColor(Color.Green) 142 .textAlign(TextAlign.Center) 143 .fontSize(30) 144 145 Text("2") 146 .width('90%') 147 .height('100%') 148 .backgroundColor(Color.Pink) 149 .textAlign(TextAlign.Center) 150 .fontSize(30) 151} 152``` 153 154![indicator](figures/indicator.PNG) 155 156 Example of customizing the style of the navigation dots indicator, with the diameter of 30 vp, left margin of 0, and color of red: 157 158```ts 159Swiper(this.swiperController) { 160 Text("0") 161 .width('90%') 162 .height('100%') 163 .backgroundColor(Color.Gray) 164 .textAlign(TextAlign.Center) 165 .fontSize(30) 166 167 Text("1") 168 .width('90%') 169 .height('100%') 170 .backgroundColor(Color.Green) 171 .textAlign(TextAlign.Center) 172 .fontSize(30) 173 174 Text("2") 175 .width('90%') 176 .height('100%') 177 .backgroundColor(Color.Pink) 178 .textAlign(TextAlign.Center) 179 .fontSize(30) 180} 181.indicatorStyle({ 182 size: 30, 183 left: 0, 184 color: Color.Red 185}) 186``` 187 188![ind](figures/ind.PNG) 189 190 191## Page Switching Mode 192 193The **\<Swiper>** component supports three page switching modes: using the swipe gesture, using the navigation dots indicator, and using the controller. 194 195 Switch pages through the controller: 196 197```ts 198@Entry 199@Component 200struct SwiperDemo { 201 private swiperController: SwiperController = new SwiperController(); 202 203 build() { 204 Column({ space: 5 }) { 205 Swiper(this.swiperController) { 206 Text("0") 207 .width(250) 208 .height(250) 209 .backgroundColor(Color.Gray) 210 .textAlign(TextAlign.Center) 211 .fontSize(30) 212 Text("1") 213 .width(250) 214 .height(250) 215 .backgroundColor(Color.Green) 216 .textAlign(TextAlign.Center) 217 .fontSize(30) 218 Text("2") 219 .width(250) 220 .height(250) 221 .backgroundColor(Color.Pink) 222 .textAlign(TextAlign.Center) 223 .fontSize(30) 224 } 225 .indicator(true) 226 227 Row({ space: 12 }) { 228 Button('showNext') 229 .onClick(() => { 230 this.swiperController.showNext(); // Switch to the next page through the controller. 231 }) 232 Button('showPrevious') 233 .onClick(() => { 234 this.swiperController.showPrevious(); // Switch to the previous page through the controller. 235 }) 236 }.margin(5) 237 }.width('100%') 238 .margin({ top: 5 }) 239 } 240} 241``` 242 243![controll](figures/controll.gif) 244 245 246## Playback Direction 247 248You can set the playback direction for the \<Swiper> component through its **vertical** attribute. 249 250When **vertical** is set to **true**, vertical swiping is used. The default value of **vertical** is **false**. 251 252 253 Example of using horizontal swiping: 254 255```ts 256Swiper(this.swiperController) { 257 ... 258} 259.indicator(true) 260.vertical(false) 261``` 262 263 264![horizontal-swiping](figures/horizontal-swiping.PNG) 265 266 267 Example of using vertical swiping: 268 269```ts 270Swiper(this.swiperController) { 271 ... 272} 273.indicator(true) 274.vertical(true) 275``` 276 277 278![vertical-swiping](figures/vertical-swiping.PNG) 279 280 281## Child Components Per Page 282 283You can set the number of child components per page for the \<Swiper> component through its [displayCount](../reference/arkui-ts/ts-container-swiper.md#attributes) attribute. 284 285 To display two child components per page: 286 287```ts 288Swiper(this.swiperController) { 289 Text("0") 290 .width(250) 291 .height(250) 292 .backgroundColor(Color.Gray) 293 .textAlign(TextAlign.Center) 294 .fontSize(30) 295 Text("1") 296 .width(250) 297 .height(250) 298 .backgroundColor(Color.Green) 299 .textAlign(TextAlign.Center) 300 .fontSize(30) 301 Text("2") 302 .width(250) 303 .height(250) 304 .backgroundColor(Color.Pink) 305 .textAlign(TextAlign.Center) 306 .fontSize(30) 307 Text("3") 308 .width(250) 309 .height(250) 310 .backgroundColor(Color.Blue) 311 .textAlign(TextAlign.Center) 312 .fontSize(30) 313} 314.indicator(true) 315.displayCount(2) 316``` 317 318![two](figures/two.PNG) 319