1# swiper开发指导 2 3 4swiper为滑动容器,提供切换显示子组件的能力。具体用法请参考[swiper](../reference/apis-arkui/arkui-js/js-components-container-swiper.md)。 5 6 7## 创建swiper组件 8 9在pages/index目录下的hml文件中创建一个swiper组件。 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 57 58 59> **说明:** 60> 61> swiper组件支持除<list>之外的子组件。 62 63 64## 添加属性 65 66swiper组件当不开启循环播放(loop="false")时添加自动播放属性(autoplay),设置自动播放时播放时间间隔(interval),页面会自动切换并停留在最后一个子组件页面。添加digital属性启用数字导航点,设置切换时为渐隐滑动效果(scrolleffect="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 118 119> **说明:** 120> - 设置indicator(是否启用导航点指示器)属性为true时digital(是否启用数字导航点)属性才会生效。 121> 122> - swiper子组件的个数大于等于2时设置的loop属性才会生效。 123> 124> - scrolleffect属性仅在loop属性值为false时生效。 125 126 127## 设置样式 128 129设置swiper组件的宽高,导航点指示器的直径大小(indicator-size)、颜色(indicator-color)、相对位置(indicator-top)及选中时的颜色(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-color: bisque;"> 137 <text>item1</text> 138 </div> 139 <div class="item" style="background-color: darkkhaki;"> 140 <text>item2</text> 141 </div> 142 <div class="item" style="background-color: cadetblue;"> 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 184 185 186## 绑定事件 187 188创建两个text组件添加点击事件,当点击后就调用showPrevious(显示上一个子组件)或showNext(显示下一个子组件)方法。添加select组件下拉选择时触发change事件后调用swipeTo方法跳转到指定轮播页面。swiper组件绑定change(当前显示的组件索引变化时触发)和finish(切换动画结束时触发)事件。 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:"切换动作结束"}); 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 286 287 288## 场景示例 289 290本场景中使用swiper创建一个轮播图,在轮播图底部制作一个缩略图,点击缩略图后调用swipeTo方法切换到对应的轮播图。 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 368 369 370## 相关实例 371 372针对swiper开发,有以下相关实例可供参考: 373 374- [简易视频播放器(JS)(API9)](https://gitee.com/openharmony/codelabs/tree/master/Media/VideoOpenHarmony) 375