自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從微信小程序到鴻蒙js開發(fā)-swiper&animator&marquee

開發(fā)
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請(qǐng)前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com/#zz

[[382656]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

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

1、swiper輪播圖

微信小程序的swiper組件中只能放置swiper-item,而鴻蒙js的swiper組件中可以放置除list之外的任意組件,功能更強(qiáng)大。除之前講過用swiper結(jié)合自定義tabbar實(shí)現(xiàn)底部菜單分頁功能,swiper最常用的就是首頁的輪播圖了。

swiper的屬性可見官方文檔(https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-components-container-swiper-0000000000611533),開發(fā)者工具在duration屬性的代碼提示是有bug的,這里應(yīng)填的是毫秒數(shù):

  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的后四個(gè)屬性所填的都是默認(rèn)值,可以省略。

2、image-animator幻燈片

swiper是滾動(dòng)輪播圖的效果,image-animator組件提供了類似幻燈片一樣的圖片切換效果。它不支持任何的子組件,且只支持圖片。官方文檔(https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-components-basic-image-animator-0000001050066126)。

image-animator的duration屬性與swiper的duration屬性不同,它支持給定單位,不給單位默認(rèn)為ms。且文檔中寫的“單次播放時(shí)長(zhǎng)”其實(shí)是一次播放完所有圖片的時(shí)長(zhǎng),每張圖片的顯示時(shí)長(zhǎng)被均分。

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

images數(shù)組格式:

  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.  ], 

支持設(shè)置fixedsize="false",即可在數(shù)組中指定每幅圖片的長(zhǎng)、寬、偏移量。

  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組件提供了一種跑馬燈的文字效果,文字從屏幕右側(cè)開始出現(xiàn),并向屏幕左側(cè)滾動(dòng)。適合做滾動(dòng)通知,或是手表類的布局。

  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: (采用動(dòng)靜分離,詳見下文)

  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動(dòng)靜分離

在這個(gè)模塊中,我并沒有將圖片放在項(xiàng)目工程目錄中,甚至圖片的url都沒有寫在js文件中。一是現(xiàn)在app功能越發(fā)強(qiáng)大,占用的存儲(chǔ)空間也越來越大,如果將靜態(tài)資源全部存放在工程目錄中加大了空間的占用量。二是如果圖片定期更換,或者服務(wù)器地址更換,寫在代碼里不便于維護(hù)。

nginx服務(wù)器可以實(shí)現(xiàn)動(dòng)靜分離,將本地路徑作為靜態(tài)資源服務(wù)器?;九渲萌缦?,在nginx.conf中添加一個(gè)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"四個(gè)子目錄,分別存放圖片、文本、視頻、音頻。重啟nginx后,訪問localhost:8500:

本地目錄就成為了靜態(tài)資源服務(wù)器,不得不感嘆nginx的強(qiáng)大。

在鴻蒙項(xiàng)目中,總不能請(qǐng)求localhost,因此再搭配內(nèi)網(wǎng)穿透,將本地服務(wù)器和域名綁定就可以了。

剛才模塊中的js代碼,就是通過請(qǐng)求靜態(tài)資源中的newyear.json文件獲取圖片路徑以及文字?jǐn)?shù)據(jù),實(shí)現(xiàn)了動(dòng)靜分離。

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""新春佳節(jié),快樂假期,祝你放假快樂,闔家幸福,新春大吉!  福氣高,樂逍遙,生活日日美,收入月月高。" 

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

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

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-02-05 09:46:16

鴻蒙HarmonyOSjs開發(fā)

2021-02-23 12:25:26

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-03-02 09:29:29

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-20 09:52:02

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-22 14:56:55

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-25 10:01:19

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-23 12:23:57

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-23 09:52:42

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-04 13:49:41

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-25 15:13:08

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-07 09:17:24

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-24 09:36:03

鴻蒙CSS應(yīng)用開發(fā)

2017-05-08 15:03:07

微信小程序開發(fā)實(shí)戰(zhàn)

2016-09-28 18:10:59

微信程序MINA

2016-09-27 16:38:24

JavaScript微信Web

2016-11-04 10:49:48

微信小程序

2016-09-27 20:36:23

微信HttpWeb

2016-11-04 10:30:17

微信小程序

2018-09-11 10:32:07

云開發(fā)小程序開發(fā)者

2016-11-07 10:30:07

微信小程序安裝配置
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)