1# swiper开发指导 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @Hu_ZeQi--> 5<!--Designer: @jiangdayuan--> 6<!--Tester: @lxl007--> 7<!--Adviser: @HelloCrease--> 8 9 10swiper为滑动容器,提供切换显示子组件的能力。具体用法请参考[swiper](../reference/apis-arkui/arkui-js/js-components-container-swiper.md)。 11 12 13## 创建swiper组件 14 15在pages/index目录下的hml文件中创建一个swiper组件。 16 17```html 18<!-- xxx.hml--> 19<div class="container"> 20 <swiper> 21 <div class="item" style="background-color: #bf45ea;"> 22 <text>item1</text> 23 </div> 24 <div class="item" style="background-color: #088684;"> 25 <text>item2</text> 26 </div> 27 <div class="item" style="background-color: #7786ee;"> 28 <text>item3</text> 29 </div> 30 </swiper> 31</div> 32``` 33 34```css 35/* xxx.css */ 36.container{ 37 width: 100%; 38 height: 100%; 39 flex-direction: column; 40 background-color: #F1F3F5; 41 align-items: center; 42 justify-content: center; 43 width: 100%; 44} 45swiper{ 46 height: 30%; 47} 48.item{ 49 width: 100%; 50 height: 500px; 51} 52text{ 53 width: 100%; 54 height: 100%; 55 text-align: center; 56 font-size: 50px; 57 color: white; 58} 59``` 60 61 62 63 64 65> **说明:** 66> 67> swiper组件支持除<list>之外的子组件。 68 69 70## 添加属性 71 72swiper组件当不开启循环播放(loop="false")时添加自动播放属性(autoplay),设置自动播放时播放时间间隔(interval),页面会自动切换并停留在最后一个子组件页面。添加digital属性启用数字导航点,设置切换时为渐隐滑动效果(scrolleffect="fade")。 73 74 75```html 76<!-- xxx.hml--> 77<div class="container"> 78 <swiper index="1" autoplay="true" interval="2000" indicator="true" digital="true" duration="500" 79 scrolleffect="fade" loop="false"> 80 <div class="item" style="background-color: #bf45ea;"> 81 <text>item1</text> 82 </div> 83 <div class="item" style="background-color: #088684;"> 84 <text>item2</text> 85 </div> 86 <div class="item" style="background-color: #7786ee;"> 87 <text>item3</text> 88 </div> 89 <div class="item" style="background-color: #c88cee;"> 90 <text>item4</text> 91 </div> 92 </swiper> 93</div> 94``` 95 96 97```css 98/* xxx.css */ 99.container{ 100 width: 100%; 101 height: 100%; 102 flex-direction: column; 103 background-color: #F1F3F5; 104 align-items: center; 105 justify-content: center; 106} 107swiper{ 108 height: 30%; 109} 110.item{ 111 width: 100%; 112 height: 500px; 113} 114text{ 115 width: 100%; 116 height: 100%; 117 text-align: center; 118 font-size: 50px; 119 color: white; 120} 121``` 122 123 124 125> **说明:** 126> - 设置indicator(是否启用导航点指示器)属性为true时digital(是否启用数字导航点)属性才会生效。 127> 128> - swiper子组件的个数大于等于2时设置的loop属性才会生效。 129> 130> - scrolleffect属性仅在loop属性值为false时生效。 131 132 133## 设置样式 134 135设置swiper组件的宽高,导航点指示器的直径大小(indicator-size)、颜色(indicator-color)、相对位置(indicator-top)及选中时的颜色(indicator-selected-color)。 136 137 138```html 139<!-- xxx.hml--> 140<div class="container"> 141 <swiper index="1" autoplay="true" interval="2000" duration="500" > 142 <div class="item" style="background-color: bisque;"> 143 <text>item1</text> 144 </div> 145 <div class="item" style="background-color: darkkhaki;"> 146 <text>item2</text> 147 </div> 148 <div class="item" style="background-color: cadetblue;"> 149 <text>item3</text> 150 </div> 151 </swiper> 152</div> 153``` 154 155 156```css 157/* xxx.css */ 158.container{ 159 width: 100%; 160 height: 100%; 161 flex-direction: column; 162 background-color: #F1F3F5; 163 align-items: center; 164 justify-content: center; 165} 166swiper{ 167 width: 500px; 168 height: 500px; 169 border-radius: 250px; 170 indicator-color: white; 171 indicator-selected-color: blue; 172 indicator-size: 40px; 173 indicator-top: 100px; 174 overflow: hidden ; 175} 176.item{ 177 width: 100%; 178 height: 500px; 179} 180text{ 181 width: 100%; 182 text-align: center; 183 margin-top: 150px; 184 font-size: 50px; 185 color: white; 186} 187``` 188 189 190 191 192## 绑定事件 193 194创建两个text组件添加点击事件,当点击后就调用showPrevious(显示上一个子组件)或showNext(显示下一个子组件)方法。添加select组件下拉选择时触发change事件后调用swipeTo方法跳转到指定轮播页面。swiper组件绑定change(当前显示的组件索引变化时触发)和finish(切换动画结束时触发)事件。 195 196 197```html 198<!-- xxx.hml--> 199<div class="container"> 200 <swiper interval="2000" onchange="change" loop="false" onanimationfinish="finish" id="swiper"> 201 <div class="item" style="background-color: #bf45ea"> 202 <text>item1</text> 203 </div> 204 <div class="item" style="background-color: #088684;"> 205 <text>item2</text> 206 </div> 207 <div class="item" style="background-color: #7786ee;"> 208 <text>item3</text> 209 </div> 210 <div class="item" style="background-color: #c88cee;"> 211 <text>item4</text> 212 </div> 213 </swiper> 214 <div class="content"> 215 <button class="pnbtn" onclick="previous">Previous</button> 216 <select onchange="selectChange"> 217 <option value="0">swipeTo 1</option> 218 <option value="1">swipeTo 2</option> 219 <option value="2">swipeTo 3</option> 220 <option value="3">swipeTo 4</option> 221 </select> 222 <button class="pnbtn" onclick="next">Next</button> 223 </div> 224</div> 225``` 226 227 228```css 229/* xxx.css */ 230.container{ 231 width: 100%; 232 height: 100%; 233 flex-direction: column; 234 background-color: #F1F3F5; 235 align-items: center; 236 justify-content: center; 237} 238swiper{ 239 height: 30%; 240} 241.item{ 242 width: 100%; 243 height: 500px; 244} 245text{ 246 width: 100%; 247 height: 100%; 248 text-align: center; 249 font-size: 50px; 250 color: white; 251} 252select{ 253 background-color: white; 254 width: 250px; 255 height: 80px; 256} 257.content{ 258 margin-top: 100px; 259 justify-content: space-around; 260} 261.pnbtn{ 262 width: 200px; 263 height: 80px; 264 font-size: 30px; 265} 266``` 267 268 269```js 270// xxx.js 271import promptAction from '@ohos.promptAction'; 272export default{ 273 change(e){ 274 promptAction.showToast({duration:2000,message:"current index:"+e.index}); 275 }, 276 finish(){ 277 promptAction.showToast({duration:2000,message:"切换动作结束"}); 278 }, 279 selectChange(e){ 280 this.$element('swiper').swipeTo({index: Number(e.newValue)}); 281 }, 282 previous(){ 283 this.$element('swiper').showPrevious(); 284 }, 285 next(){ 286 this.$element('swiper').showNext(); 287 } 288} 289``` 290 291 292 293 294## 场景示例 295 296本场景中使用swiper创建一个轮播图,在轮播图底部制作一个缩略图,点击缩略图后调用swipeTo方法切换到对应的轮播图。 297 298 299```html 300<!-- xxx.hml--> 301<div class="container"> 302 <swiper duration="500" indicator="false" id="swiper" onchange="change"> 303 <div class="item" for="item in list"> 304 <image src="{{item.src}}"></image> 305 </div> 306 </swiper> 307 <div class="content"> 308 <div class="content_item {{index == $idx?'actived':''}}" for="item in list" onclick="imageTo({{$idx}})"> 309 <image src="{{item.src}}"></image> 310 </div> 311 </div> 312</div> 313``` 314 315 316```css 317/* xxx.css */ 318.container{ 319 flex-direction: column; 320 background-color: #F1F3F5; 321 align-items: center; 322 justify-content: center; 323 width: 100%; 324} 325swiper{ 326 width: 100%; 327 height: 500px; 328} 329.item{ 330 width: 100%; 331 height: 500px; 332} 333.content{ 334 margin-top: -120px; 335 width: 70%; 336 display: flex; 337 justify-content: space-around; 338 height: 100px; 339} 340.content_item{ 341 padding: 5px; 342 transform: scale(0.5); 343} 344.actived{ 345 transform: scale(1); 346 border: 1px solid #b20937ea; 347} 348``` 349 350 351```js 352// xxx.js 353import promptAction from '@ohos.promptAction'; 354export default { 355 data:{ 356 index: 0, 357 list:[ 358 {src: 'common/images/1.png'}, 359 {src: 'common/images/2.png'}, 360 {src: 'common/images/3.png'}, 361 {src: 'common/images/4.png'},] 362 }, 363 imageTo(index){ 364 this.index = index; 365 this.$element('swiper').swipeTo({index: index}); 366 }, 367 change(e){ 368 this.index = e.index; 369 } 370} 371``` 372 373 374 375 376## 相关实例 377 378针对swiper开发,有以下相关实例可供参考: 379 380- [简易视频播放器(JS)(API9)](https://gitee.com/openharmony/codelabs/tree/master/Media/VideoOpenHarmony) 381