1# <swiper> Development 2 3 4The **<swiper>** component is a sliding container used to switch between child components. For details, see [swiper](../reference/arkui-js/js-components-container-swiper.md). 5 6 7## Creating a <swiper> Component 8 9Create a **<swiper>** component in the .hml file under **pages/index**. 10 11```html 12<!-- xxx.hml--> 13<div class="container"> 14 <swiper> 15 <div class="item" style="background-color: #bf45ea;"> 16 <text>item1</text> 17 </div> 18 <div class="item" style="background-color: #088684;"> 19 <text>item2</text> 20 </div> 21 <div class="item" style="background-color: #7786ee;"> 22 <text>item3</text> 23 </div> 24 </swiper> 25</div> 26``` 27 28```css 29/* xxx.css */ 30.container{ 31 width: 100%; 32 height: 100%; 33 flex-direction: column; 34 background-color: #F1F3F5; 35 align-items: center; 36 justify-content: center; 37 width: 100%; 38} 39swiper{ 40 height: 30%; 41} 42.item{ 43 width: 100%; 44 height: 500px; 45} 46text{ 47 width: 100%; 48 height: 100%; 49 text-align: center; 50 font-size: 50px; 51 color: white; 52} 53``` 54 55 56![en-us_image_0000001232003028](figures/en-us_image_0000001232003028.gif) 57 58 59> **NOTE** 60> 61> The **<swiper>** component supports child components except **<list>**. 62 63 64## Adding Attributes 65 66When **loop** is set to **false**, the **autoplay** attribute is added to the **<swiper>** component and the autoplay interval (**interval**) is set. The component automatically switches between child components and stops at the last one. Add the **digital** attribute to enable the digital navigation point and set **scrolleffect** to **fade**. 67 68 69```html 70<!-- xxx.hml--> 71<div class="container"> 72 <swiper index="1" autoplay="true" interval="2000" indicator="true" digital="true" duration="500" 73 scrolleffect="fade" loop="false"> 74 <div class="item" style="background-color: #bf45ea;"> 75 <text>item1</text> 76 </div> 77 <div class="item" style="background-color: #088684;"> 78 <text>item2</text> 79 </div> 80 <div class="item" style="background-color: #7786ee;"> 81 <text>item3</text> 82 </div> 83 <div class="item" style="background-color: #c88cee;"> 84 <text>item4</text> 85 </div> 86 </swiper> 87</div> 88``` 89 90 91```css 92/* xxx.css */ 93.container{ 94 width: 100%; 95 height: 100%; 96 flex-direction: column; 97 background-color: #F1F3F5; 98 align-items: center; 99 justify-content: center; 100} 101swiper{ 102 height: 30%; 103} 104.item{ 105 width: 100%; 106 height: 500px; 107} 108text{ 109 width: 100%; 110 height: 100%; 111 text-align: center; 112 font-size: 50px; 113 color: white; 114} 115``` 116 117![en-us_image_0000001275923021](figures/en-us_image_0000001275923021.gif) 118 119> **NOTE** 120> - The **digital** attribute takes effect only when the **indicator** attribute is set to **true**. 121> 122> - The **loop** attribute takes effect only when there are two or more than two child components of the **<swiper>** component. 123> 124> - The **scrolleffect** attribute takes effect only when the **loop** attribute value is set to **false**. 125 126 127## Setting Styles 128 129Set the width and height of the **<swiper>** component, the indicator's size (**indicator-size**), color (**indicator-color**), relative position (**indicator-top**), and color when it is selected (**indicator-selected-color**). 130 131 132```html 133<!-- xxx.hml--> 134<div class="container"> 135 <swiper index="1" autoplay="true" interval="2000" duration="500" > 136 <div class="item" style="background: linear-gradient(to right,#806dd9,#5d44ea,#2eb9d5)"> 137 <text>item1</text> 138 </div> 139 <div class="item" style="background: linear-gradient( to right,#2eb9d5,#0e7db4,#2673d9)"> 140 <text>item2</text> 141 </div> 142 <div class="item" style="background: linear-gradient( to right,#2673d9,#0c89af,#806dd9)"> 143 <text>item3</text> 144 </div> 145 </swiper> 146</div> 147``` 148 149 150```css 151/* xxx.css */ 152.container{ 153 width: 100%; 154 height: 100%; 155 flex-direction: column; 156 background-color: #F1F3F5; 157 align-items: center; 158 justify-content: center; 159} 160swiper{ 161 width: 500px; 162 height: 500px; 163 border-radius: 250px; 164 indicator-color: white; 165 indicator-selected-color: blue; 166 indicator-size: 40px; 167 indicator-top: 100px; 168 overflow: hidden ; 169} 170.item{ 171 width: 100%; 172 height: 500px; 173} 174text{ 175 width: 100%; 176 text-align: center; 177 margin-top: 150px; 178 font-size: 50px; 179 color: white; 180} 181``` 182 183![en-us_image_0000001275803189](figures/en-us_image_0000001275803189.gif) 184 185 186## Binding Events 187 188Create two **<text>** components and bind click events. Clicking the component will call **showPrevious** to display the previous child component or **showNext** to display the next child component. Add a **<select>** component. A **change** event is triggered when a user selects a value from the drop-down list box and the **swipeTo** method is called to go to the specified page. Bind the **<swiper>** component with the **change** event (triggered when the index of the currently displayed component changes) and the **finish** event (triggered when the switchover animation ends). 189 190 191```html 192<!-- xxx.hml--> 193<div class="container"> 194 <swiper interval="2000" onchange="change" loop="false" onanimationfinish="finish" id="swiper"> 195 <div class="item" style="background-color: #bf45ea"> 196 <text>item1</text> 197 </div> 198 <div class="item" style="background-color: #088684;"> 199 <text>item2</text> 200 </div> 201 <div class="item" style="background-color: #7786ee;"> 202 <text>item3</text> 203 </div> 204 <div class="item" style="background-color: #c88cee;"> 205 <text>item4</text> 206 </div> 207 </swiper> 208 <div class="content"> 209 <button class="pnbtn" onclick="previous">Previous</button> 210 <select onchange="selectChange"> 211 <option value="0">swipeTo 1</option> 212 <option value="1">swipeTo 2</option> 213 <option value="2">swipeTo 3</option> 214 <option value="3">swipeTo 4</option> 215 </select> 216 <button class="pnbtn" onclick="next">Next</button> 217 </div> 218</div> 219``` 220 221 222```css 223/* xxx.css */ 224.container{ 225 width: 100%; 226 height: 100%; 227 flex-direction: column; 228 background-color: #F1F3F5; 229 align-items: center; 230 justify-content: center; 231} 232swiper{ 233 height: 30%; 234} 235.item{ 236 width: 100%; 237 height: 500px; 238} 239text{ 240 width: 100%; 241 height: 100%; 242 text-align: center; 243 font-size: 50px; 244 color: white; 245} 246select{ 247 background-color: white; 248 width: 250px; 249 height: 80px; 250} 251.content{ 252 margin-top: 100px; 253 justify-content: space-around; 254} 255.pnbtn{ 256 width: 200px; 257 height: 80px; 258 font-size: 30px; 259} 260``` 261 262 263```js 264// xxx.js 265import promptAction from '@ohos.promptAction'; 266export default{ 267 change(e){ 268 promptAction.showToast({duration:2000,message:"current index:"+e.index}); 269 }, 270 finish(){ 271 promptAction.showToast({duration:2000,message:"The switchover ends"}); 272 }, 273 selectChange(e){ 274 this.$element('swiper').swipeTo({index: Number(e.newValue)}); 275 }, 276 previous(){ 277 this.$element('swiper').showPrevious(); 278 }, 279 next(){ 280 this.$element('swiper').showNext(); 281 } 282} 283``` 284 285![en-us_image_0000001231843128](figures/en-us_image_0000001231843128.gif) 286 287 288## Example Scenario 289 290Use the **<swiper>** component to create an image carousel and a thumbnail module at the bottom of the carousel. After a thumbnail is clicked, the **swipeTo** method is called to switch to the corresponding image. 291 292 293```html 294<!-- xxx.hml--> 295<div class="container"> 296 <swiper duration="500" indicator="false" id="swiper" onchange="change"> 297 <div class="item" for="item in list"> 298 <image src="{{item.src}}"></image> 299 </div> 300 </swiper> 301 <div class="content"> 302 <div class="content_item {{index == $idx?'actived':''}}" for="item in list" onclick="imageTo({{$idx}})"> 303 <image src="{{item.src}}"></image> 304 </div> 305 </div> 306</div> 307``` 308 309 310```css 311/* xxx.css */ 312.container{ 313 flex-direction: column; 314 background-color: #F1F3F5; 315 align-items: center; 316 justify-content: center; 317 width: 100%; 318} 319swiper{ 320 width: 100%; 321 height: 500px; 322} 323.item{ 324 width: 100%; 325 height: 500px; 326} 327.content{ 328 margin-top: -120px; 329 width: 70%; 330 display: flex; 331 justify-content: space-around; 332 height: 100px; 333} 334.content_item{ 335 padding: 5px; 336 transform: scale(0.5); 337} 338.actived{ 339 transform: scale(1); 340 border: 1px solid #b20937ea; 341} 342``` 343 344 345```js 346// xxx.js 347import promptAction from '@ohos.promptAction'; 348export default { 349 data:{ 350 index: 0, 351 list:[ 352 {src: 'common/images/1.png'}, 353 {src: 'common/images/2.png'}, 354 {src: 'common/images/3.png'}, 355 {src: 'common/images/4.png'},] 356 }, 357 imageTo(index){ 358 this.index = index; 359 this.$element('swiper').swipeTo({index: index}); 360 }, 361 change(e){ 362 this.index = e.index; 363 } 364} 365``` 366 367![en-us_image_0000001231843132](figures/en-us_image_0000001231843132.gif)