从微信小程序到鸿蒙js开发-swiper&animator&marquee

开发
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com/#zz

[[382656]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

1、swiper轮播图

微信小程序的swiper组件中只能放置swiper-item,而鸿蒙js的swiper组件中可以放置除list之外的任意组件,功能更强大。除之前讲过用swiper结合自定义tabbar实现底部菜单分页功能,swiper最常用的就是首页的轮播图了。

swiper的属性可见官方文档(https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-components-container-swiper-0000000000611533),开发者工具在duration属性的代码提示是有bug的,这里应填的是毫秒数:

  1. <swiper autoplay="true" duration="1000" interval="3000" indicator="true" loop="true" vertical="false"
  2.       <block for="{{ swipeImg }}"
  3.           <image src="{{ $item }}"></image> 
  4.       </block> 
  5.   </swiper> 

代码中swiper的后四个属性所填的都是默认值,可以省略。

2、image-animator幻灯片

swiper是滚动轮播图的效果,image-animator组件提供了类似幻灯片一样的图片切换效果。它不支持任何的子组件,且只支持图片。官方文档(https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-components-basic-image-animator-0000001050066126)。

image-animator的duration属性与swiper的duration属性不同,它支持给定单位,不给单位默认为ms。且文档中写的“单次播放时长”其实是一次播放完所有图片的时长,每张图片的显示时长被均分。

  1. <image-animator duration="8s" images="{{ animatorImg }}"></image-animator> 

images数组格式:

  1. "animatorImg": [ 
  2.      { 
  3.          "src""newyear1.jpeg" 
  4.      }, 
  5.      { 
  6.          "src""newyear2.jpeg" 
  7.      }, 
  8.      { 
  9.          "src""newyear3.jpeg" 
  10.      }, 
  11.      { 
  12.          "src""newyear4.jpeg" 
  13.      } 
  14.  ], 

支持设置fixedsize="false",即可在数组中指定每幅图片的长、宽、偏移量。

  1. <image-animator duration="8s" images="{{ animatorImg }}" fixedsize="false"></image-animator> 

  1. "animatorImg": [ 
  2.     { 
  3.         "src""newyear1.jpeg"
  4.         "width": 500, 
  5.         "height": 500 
  6.     }, 
  7.     { 
  8.         "src""newyear2.jpeg" 
  9.     }, 
  10.     { 
  11.         "src""newyear3.jpeg" 
  12.     }, 
  13.     { 
  14.         "src""newyear4.jpeg"
  15.         "width": 400, 
  16.         "height": 400, 
  17.         "top": 100, 
  18.         "left": 100 
  19.     } 
  20. ], 

3、marquee跑马灯

marquee组件提供了一种跑马灯的文字效果,文字从屏幕右侧开始出现,并向屏幕左侧滚动。适合做滚动通知,或是手表类的布局。

  1. <marquee> 
  2.     {{ text }} 
  3. </marquee> 

整体代码和效果图:

hml:

  1. <div class="container"
  2.     <swiper autoplay="true" duration="1000" interval="3000" indicator="true" loop="true" vertical="false"
  3.         <block for="{{ swipeImg }}"
  4.             <image src="{{ $item }}"></image> 
  5.         </block> 
  6.     </swiper> 
  7.     <marquee> 
  8.         {{ text }} 
  9.     </marquee> 
  10.     <image-animator duration="8s" images="{{ animatorImg }}" fixedsize="false"></image-animator> 
  11. </div> 

css:

  1. .container { 
  2.     display: flex; 
  3.     flex-direction: column
  4.     width: 100%; 
  5.     height: 1200px; 
  6. swiper { 
  7.     width: 100%; 
  8.     height: 350px; 
  9. swiper image { 
  10.     width: 100%; 
  11.     height: 350px; 
  12.  
  13. marquee { 
  14.     margin-top: 20px; 
  15.     margin-bottom: 20px; 
  16.     width: 100%; 
  17.  
  18. image-animator { 
  19.     width: 100%; 
  20.     height: 550px; 

js: (采用动静分离,详见下文)

  1. import fetch from '@system.fetch'
  2.  
  3. export default { 
  4.     data: { 
  5.         dataUrl: "http://milkytea.free.idcfengye.com/text/newyear.json"
  6.         swipeImg: [], 
  7.         text: ""
  8.         animatorImg: [] 
  9.     }, 
  10.     onInit() { 
  11.         fetch.fetch({ 
  12.             url: this.dataUrl, 
  13.             responseType: 'json'
  14.             success: res => { 
  15.                 let data = JSON.parse(res.data); 
  16.                 let imgUrl = data.imgUrl; 
  17.                 let swipeImg = data.swipeImg; 
  18.                 let animatorImg = data.animatorImg; 
  19.                 for (let i in swipeImg) { 
  20.                     swipeImg[i] = imgUrl + swipeImg[i]; 
  21.                 } 
  22.                 for (let i in animatorImg) { 
  23.                     animatorImg[i].src = imgUrl + animatorImg[i].src; 
  24.                 } 
  25.                 this.swipeImg = swipeImg; 
  26.                 this.text = data.text; 
  27.                 this.animatorImg = animatorImg; 
  28.             } 
  29.         }) 
  30.     } 

4、nginx动静分离

在这个模块中,我并没有将图片放在项目工程目录中,甚至图片的url都没有写在js文件中。一是现在app功能越发强大,占用的存储空间也越来越大,如果将静态资源全部存放在工程目录中加大了空间的占用量。二是如果图片定期更换,或者服务器地址更换,写在代码里不便于维护。

nginx服务器可以实现动静分离,将本地路径作为静态资源服务器。基本配置如下,在nginx.conf中添加一个server:

  1. server{ 
  2.       listen 8500; 
  3.       server_name localhost; 
  4.  
  5.       location / { 
  6.           root /Users/liuyufeng/image/; 
  7.           autoindex on
  8.       } 
  9.  
  10.       location ~ ^/(images|text|video|audio)/ { 
  11.           root /Users/liuyufeng/image/; 
  12.           autoindex on
  13.           access_log on
  14.           expires 30d; 
  15.       } 
  16.   } 

将本地文件夹"/Users/liuyufeng/image"和localhost:8500绑定,并通过正则匹配"images","text","video","audio"四个子目录,分别存放图片、文本、视频、音频。重启nginx后,访问localhost:8500:

本地目录就成为了静态资源服务器,不得不感叹nginx的强大。

在鸿蒙项目中,总不能请求localhost,因此再搭配内网穿透,将本地服务器和域名绑定就可以了。

刚才模块中的js代码,就是通过请求静态资源中的newyear.json文件获取图片路径以及文字数据,实现了动静分离。

newyear.json

  1.     "imgUrl""http://milkytea.free.idcfengye.com/images/newyear/"
  2.     "swipeImg": ["swiper1.jpg""swiper2.jpg""swiper3.jpg"], 
  3.     "animatorImg": [ 
  4.         { 
  5.             "src""newyear1.jpeg"
  6.             "width": 500, 
  7.             "height": 500 
  8.         }, 
  9.         { 
  10.             "src""newyear2.jpeg" 
  11.         }, 
  12.         { 
  13.             "src""newyear3.jpeg" 
  14.         }, 
  15.         { 
  16.             "src""newyear4.jpeg"
  17.             "width": 400, 
  18.             "height": 400, 
  19.             "top": 100, 
  20.             "left": 100 
  21.         } 
  22.     ], 
  23.     "text""新春佳节,快乐假期,祝你放假快乐,阖家幸福,新春大吉!  福气高,乐逍遥,生活日日美,收入月月高。" 

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-02-05 09:46:16

鸿蒙HarmonyOSjs开发

2021-02-23 12:25:26

鸿蒙HarmonyOS应用开发

2021-03-02 09:29:29

鸿蒙HarmonyOS应用开发

2021-02-20 09:52:02

鸿蒙HarmonyOS应用开发

2021-02-25 10:01:19

鸿蒙HarmonyOS应用开发

2021-02-22 14:56:55

鸿蒙HarmonyOS应用开发

2021-02-23 12:23:57

鸿蒙HarmonyOS应用开发

2021-02-23 09:52:42

鸿蒙HarmonyOS应用开发

2021-02-04 13:49:41

鸿蒙HarmonyOS应用开发

2021-02-25 15:13:08

鸿蒙HarmonyOS应用开发

2021-02-24 09:36:03

鸿蒙CSS应用开发

2021-02-07 09:17:24

鸿蒙HarmonyOS应用开发

2017-05-08 15:03:07

微信小程序开发实战

2016-09-27 16:38:24

JavaScript微信Web

2016-11-04 10:49:48

微信小程序

2016-09-28 18:10:59

微信程序MINA

2016-09-27 20:36:23

微信HttpWeb

2016-11-04 10:30:17

微信小程序

2018-09-11 10:32:07

云开发小程序开发者

2016-11-07 10:30:07

微信小程序安装配置
点赞
收藏

51CTO技术栈公众号