1# Creating a Swiper (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... 22export let 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 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 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```ts 89Swiper(this.swiperController) { 90 Text("0") 91 .width('90%') 92 .height('100%') 93 .backgroundColor(Color.Gray) 94 .textAlign(TextAlign.Center) 95 .fontSize(30) 96 97 Text("1") 98 .width('90%') 99 .height('100%') 100 .backgroundColor(Color.Green) 101 .textAlign(TextAlign.Center) 102 .fontSize(30) 103 104 Text("2") 105 .width('90%') 106 .height('100%') 107 .backgroundColor(Color.Pink) 108 .textAlign(TextAlign.Center) 109 .fontSize(30) 110} 111.loop(true) 112.autoPlay(true) 113.interval(1000) 114``` 115 116 117 118 119## Navigation Dots Indicator 120 121The **\<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. 122 123With 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. 124 125- Example of using the navigation dots indicator in its default style 126 127```ts 128Swiper(this.swiperController) { 129 Text("0") 130 .width('90%') 131 .height('100%') 132 .backgroundColor(Color.Gray) 133 .textAlign(TextAlign.Center) 134 .fontSize(30) 135 136 Text("1") 137 .width('90%') 138 .height('100%') 139 .backgroundColor(Color.Green) 140 .textAlign(TextAlign.Center) 141 .fontSize(30) 142 143 Text("2") 144 .width('90%') 145 .height('100%') 146 .backgroundColor(Color.Pink) 147 .textAlign(TextAlign.Center) 148 .fontSize(30) 149} 150``` 151 152 153 154- Example of customizing the style of the navigation dots indicator, with the diameter of 30 vp, left margin of 0, and color of red 155 156```ts 157let swco:Record<string,number|Color> = {'size':30,'left':0,'color':Color.Red} 158Swiper(this.swiperController) { 159 Text("0") 160 .width('90%') 161 .height('100%') 162 .backgroundColor(Color.Gray) 163 .textAlign(TextAlign.Center) 164 .fontSize(30) 165 166 Text("1") 167 .width('90%') 168 .height('100%') 169 .backgroundColor(Color.Green) 170 .textAlign(TextAlign.Center) 171 .fontSize(30) 172 173 Text("2") 174 .width('90%') 175 .height('100%') 176 .backgroundColor(Color.Pink) 177 .textAlign(TextAlign.Center) 178 .fontSize(30) 179} 180.indicatorStyle(swco) 181``` 182 183 184 185 186## Page Switching Mode 187 188The **\<Swiper>** component supports three page switching modes: using the swipe gesture, using the navigation dots indicator, and using the controller. The following example shows how to switch pages using the controller. 189 190```ts 191@Entry 192@Component 193struct SwiperDemo { 194 private swiperController: SwiperController = new SwiperController(); 195 196 build() { 197 Column({ space: 5 }) { 198 Swiper(this.swiperController) { 199 Text("0") 200 .width(250) 201 .height(250) 202 .backgroundColor(Color.Gray) 203 .textAlign(TextAlign.Center) 204 .fontSize(30) 205 Text("1") 206 .width(250) 207 .height(250) 208 .backgroundColor(Color.Green) 209 .textAlign(TextAlign.Center) 210 .fontSize(30) 211 Text("2") 212 .width(250) 213 .height(250) 214 .backgroundColor(Color.Pink) 215 .textAlign(TextAlign.Center) 216 .fontSize(30) 217 } 218 .indicator(true) 219 220 Row({ space: 12 }) { 221 Button('showNext') 222 .onClick(() => { 223 this.swiperController.showNext(); // Switch to the next page through the controller. 224 }) 225 Button('showPrevious') 226 .onClick(() => { 227 this.swiperController.showPrevious(); // Switch to the previous page through the controller. 228 }) 229 }.margin(5) 230 }.width('100%') 231 .margin({ top: 5 }) 232 } 233} 234``` 235 236 237 238 239## Playback Direction 240 241You can set the playback direction for the \<Swiper> component through its **vertical** attribute. 242 243When **vertical** is set to **true**, vertical swiping is used. The default value of **vertical** is **false**. 244 245 246- Example of using horizontal swiping 247 248```ts 249Swiper(this.swiperController) { 250 ... 251} 252.indicator(true) 253.vertical(false) 254``` 255 256 257 258 259 260- Example of using vertical swiping 261 262```ts 263Swiper(this.swiperController) { 264 ... 265} 266.indicator(true) 267.vertical(true) 268``` 269 270 271 272 273 274## Child Components Per Page 275 276You 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. 277 278```ts 279Swiper(this.swiperController) { 280 Text("0") 281 .width(250) 282 .height(250) 283 .backgroundColor(Color.Gray) 284 .textAlign(TextAlign.Center) 285 .fontSize(30) 286 Text("1") 287 .width(250) 288 .height(250) 289 .backgroundColor(Color.Green) 290 .textAlign(TextAlign.Center) 291 .fontSize(30) 292 Text("2") 293 .width(250) 294 .height(250) 295 .backgroundColor(Color.Pink) 296 .textAlign(TextAlign.Center) 297 .fontSize(30) 298 Text("3") 299 .width(250) 300 .height(250) 301 .backgroundColor(Color.Blue) 302 .textAlign(TextAlign.Center) 303 .fontSize(30) 304} 305.indicator(true) 306.displayCount(2) 307``` 308 309 310