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