1# Creating a Swiper (Swiper) 2 3 4The [\<Swiper>](../reference/apis-arkui/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 6The **\<Swiper>** component provides a preloading mechanism, which you can use to improve the swipe experience in complex scenarios. This mechanism allows for prebuilding and prerendering components when the main thread is idle. For details, see [Swiper High-Performance Development](../performance/swiper_optimization.md). 7 8 9## Layout and Constraints 10 11The **\<Swiper>** component follows its own size settings if they are configured. If the component does not have its own size settings configured, it follows the size of its parent component when the **prevMargin** or **nextMargin** attribute is set, or adapts its size to its child components otherwise. 12 13 14## Loop Playback 15 16The **loop** attribute sets whether to enable loop playback. Its default value is **true**. 17 18When **loop** is set to **true**, the user can switch to the previous or next page when they are on the first or last page. 19 20- Example of setting **loop** to **true**: 21 22```ts 23... 24private swiperController: SwiperController = new SwiperController() 25... 26Swiper(this.swiperController) { 27 Text('0') 28 .width('90%') 29 .height('100%') 30 .backgroundColor(Color.Gray) 31 .textAlign(TextAlign.Center) 32 .fontSize(30) 33 34 Text('1') 35 .width('90%') 36 .height('100%') 37 .backgroundColor(Color.Green) 38 .textAlign(TextAlign.Center) 39 .fontSize(30) 40 41 Text('2') 42 .width('90%') 43 .height('100%') 44 .backgroundColor(Color.Pink) 45 .textAlign(TextAlign.Center) 46 .fontSize(30) 47} 48.loop(true) 49``` 50 51 52 53- Example of setting **loop** to **false**: 54 55```ts 56Swiper(this.swiperController) { 57 Text('0') 58 .width('90%') 59 .height('100%') 60 .backgroundColor(Color.Gray) 61 .textAlign(TextAlign.Center) 62 .fontSize(30) 63 64 Text('1') 65 .width('90%') 66 .height('100%') 67 .backgroundColor(Color.Green) 68 .textAlign(TextAlign.Center) 69 .fontSize(30) 70 71 Text('2') 72 .width('90%') 73 .height('100%') 74 .backgroundColor(Color.Pink) 75 .textAlign(TextAlign.Center) 76 .fontSize(30) 77} 78.loop(false) 79``` 80 81 82 83 84## Automatic Playback 85 86The **autoPlay** attribute sets whether to enable automatic playback for child component switching. Its default value is **false**. 87 88When **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. 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 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 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 159 160```ts 161let swco:Record<string, number | Color> = {'size':30,'left':0,'color':Color.Red} 162Swiper(this.swiperController) { 163 Text('0') 164 .width('90%') 165 .height('100%') 166 .backgroundColor(Color.Gray) 167 .textAlign(TextAlign.Center) 168 .fontSize(30) 169 170 Text('1') 171 .width('90%') 172 .height('100%') 173 .backgroundColor(Color.Green) 174 .textAlign(TextAlign.Center) 175 .fontSize(30) 176 177 Text('2') 178 .width('90%') 179 .height('100%') 180 .backgroundColor(Color.Pink) 181 .textAlign(TextAlign.Center) 182 .fontSize(30) 183} 184.indicator( 185 Indicator.dot() 186 .left(0) 187 .itemWidth(15) 188 .itemHeight(15) 189 .selectedItemWidth(30) 190 .selectedItemHeight(15) 191 .color(Color.Red) 192 .selectedColor(Color.Blue) 193) 194``` 195 196 197 198 199## Page Switching Mode 200 201The **\<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. 202 203```ts 204@Entry 205@Component 206struct SwiperDemo { 207 private swiperController: SwiperController = new SwiperController(); 208 209 build() { 210 Column({ space: 5 }) { 211 Swiper(this.swiperController) { 212 Text('0') 213 .width(250) 214 .height(250) 215 .backgroundColor(Color.Gray) 216 .textAlign(TextAlign.Center) 217 .fontSize(30) 218 Text('1') 219 .width(250) 220 .height(250) 221 .backgroundColor(Color.Green) 222 .textAlign(TextAlign.Center) 223 .fontSize(30) 224 Text('2') 225 .width(250) 226 .height(250) 227 .backgroundColor(Color.Pink) 228 .textAlign(TextAlign.Center) 229 .fontSize(30) 230 } 231 .indicator(true) 232 233 Row({ space: 12 }) { 234 Button('showNext') 235 .onClick(() => { 236 this.swiperController.showNext(); // Switch to the next page through the controller. 237 }) 238 Button('showPrevious') 239 .onClick(() => { 240 this.swiperController.showPrevious(); // Switch to the previous page through the controller. 241 }) 242 }.margin(5) 243 }.width('100%') 244 .margin({ top: 5 }) 245 } 246} 247``` 248 249 250 251 252## Playback Direction 253 254You can set the playback direction for the \<Swiper> component through its **vertical** attribute. 255 256When **vertical** is set to **true**, vertical swiping is used. The default value of **vertical** is **false**. 257 258 259- Example of using horizontal swiping: 260 261```ts 262Swiper(this.swiperController) { 263 ... 264} 265.indicator(true) 266.vertical(false) 267``` 268 269 270 271 272 273- Example of using vertical swiping: 274 275```ts 276Swiper(this.swiperController) { 277 ... 278} 279.indicator(true) 280.vertical(true) 281``` 282 283 284 285 286 287## Child Components Per Page 288 289You can set the number of child components per page for the **\<Swiper>** component through its [displayCount](../reference/apis-arkui/arkui-ts/ts-container-swiper.md#attributes) attribute. 290 291```ts 292Swiper(this.swiperController) { 293 Text('0') 294 .width(250) 295 .height(250) 296 .backgroundColor(Color.Gray) 297 .textAlign(TextAlign.Center) 298 .fontSize(30) 299 Text('1') 300 .width(250) 301 .height(250) 302 .backgroundColor(Color.Green) 303 .textAlign(TextAlign.Center) 304 .fontSize(30) 305 Text('2') 306 .width(250) 307 .height(250) 308 .backgroundColor(Color.Pink) 309 .textAlign(TextAlign.Center) 310 .fontSize(30) 311 Text('3') 312 .width(250) 313 .height(250) 314 .backgroundColor(Color.Blue) 315 .textAlign(TextAlign.Center) 316 .fontSize(30) 317} 318.indicator(true) 319.displayCount(2) 320``` 321 322 323