鴻蒙OS 開發(fā)一個JS FA應(yīng)用

2020-09-18 13:40 更新

本章節(jié)主要介紹如何開發(fā)一個 JS FA 應(yīng)用。此應(yīng)用相對于 Hello World 應(yīng)用模板具備更復(fù)雜的頁面布局、頁面樣式和頁面邏輯。該頁面可以通過將焦點移動到不同顏色的圓形來選擇不同的食物圖片,也可以進(jìn)行添加到購物車操作,應(yīng)用效果圖如下。

圖1 JS FA應(yīng)用效果圖 點擊放大

構(gòu)建頁面布局

開發(fā)者在 index.hml 文件中構(gòu)建頁面布局。在進(jìn)行代碼開發(fā)之前,首先要對頁面布局進(jìn)行分析,將頁面分解為不同的部分,用容器組件來承載。根據(jù) JS FA 應(yīng)用效果圖,此頁面一共分成三個部分:標(biāo)題區(qū)、展示區(qū)和購物車區(qū)。根據(jù)此分區(qū),可以確定根節(jié)點的子節(jié)點應(yīng)按列排列。

標(biāo)題區(qū)是由兩個按列排列的 tex t組件實現(xiàn),購物車區(qū)由一個 text 組件構(gòu)成。展示區(qū)由按行排列的 swiper 組件和 div 組件組成,如下圖所示:

  • 第一部分是由一個容器組件 swiper,包含了四個image 組件構(gòu)成;
  • 第二部分是由一個容器組件 div,包含了一個 text 組件和四個畫布組件 canvas 繪制的圓形構(gòu)成。

圖2 展示區(qū)布局 點擊放大

根據(jù)布局結(jié)構(gòu)的分析,實現(xiàn)頁面基礎(chǔ)布局的代碼示例如下(其中四個 image 組件和 canvas 組件通過 for 指令來循環(huán)創(chuàng)建):

  1. <!-- index.hml -->
  2. <div class="container">
  3. <div class="title-section">
  4. <div class="title">
  5. <text class="name">Food</text>
  6. <text class="sub-title">Choose What You Like</text>
  7. </div>
  8. </div>
  9. <div>
  10. <swiper id="swiperImage" class="swiper-style">
  11. <image src="{{$item}}" class="image-mode" focusable="true" for="{{imageList}}"></image>
  12. </swiper>
  13. <div class="container">
  14. <div class="description-first-paragraph">
  15. <text class="description">{{descriptionFirstParagraph}}</text>
  16. </div>
  17. <div class="color-column">
  18. <canvas id="{{$item.id}}" onfocus="swipeToIndex({{$item.index}})" class="color-item" focusable="true"
  19. for="{{canvasList}}"></canvas>
  20. </div>
  21. </div>
  22. </div>
  23. <div class="cart">
  24. <text class="{{cartStyle}}" onclick="addCart" onfocus="getFocus" onblur="lostFocus" focusable="true">
  25. {{cartText}}</text>
  26. </div>
  27. </div>

說明

common 目錄用于存放公共資源,swiper 組件里展示的圖片需要放在 common 目錄下。

構(gòu)建頁面樣式

開發(fā)者在 index.css 文件中需要設(shè)定的樣式主要有 flex-direction(主軸方向),padding(內(nèi)邊距), font-size(字體大小)等。在構(gòu)建頁面樣式中,還采用了 css 偽類的寫法,當(dāng)焦點移動到 canvas 組件上時,背景顏色變成白色,也可以在 js 中通過 focus 和 blur 事件動態(tài)修改 css 樣式來實現(xiàn)同樣的效果。

  1. /* index.css */
  2. .container {
  3. flex-direction: column;
  4. }
  5. .title-section {
  6. flex-direction: row;
  7. height: 60px;
  8. margin-bottom: 5px;
  9. margin-top: 10px;
  10. }
  11. .title {
  12. align-items: flex-start;
  13. flex-direction: column;
  14. padding-left: 60px;
  15. padding-right: 160px;
  16. }
  17. .name {
  18. font-size: 20px;
  19. }
  20. .sub-title {
  21. font-size: 15px;
  22. color: #7a787d;
  23. margin-top: 10px;
  24. }
  25. .swiper-style {
  26. height: 250px;
  27. width: 350px;
  28. indicator-color: #4682b4;
  29. indicator-selected-color: #f0e68c;
  30. indicator-size: 10px;
  31. margin-left: 50px;
  32. }
  33. .image-mode {
  34. object-fit: contain;
  35. }
  36. .color-column {
  37. flex-direction: row;
  38. align-content: center;
  39. margin-top: 20px;
  40. }
  41. .color-item {
  42. height: 50px;
  43. width: 50px;
  44. margin-left: 50px;
  45. padding-left: 10px;
  46. }
  47. .color-item:focus {
  48. background-color: white;
  49. }
  50. .description-first-paragraph {
  51. padding-left: 60px;
  52. padding-right: 60px;
  53. padding-top: 30px;
  54. }
  55. .description {
  56. color: #7a787d;
  57. font-size: 15px;
  58. }
  59. .cart {
  60. justify-content: center;
  61. margin-top: 20px;
  62. }
  63. .cart-text {
  64. font-size: 20px;
  65. text-align: center;
  66. width: 300px;
  67. height: 50px;
  68. background-color: #6495ed;
  69. color: white;
  70. }
  71. .cart-text-focus {
  72. font-size: 20px;
  73. text-align: center;
  74. width: 300px;
  75. height: 50px;
  76. background-color: #4169e1;
  77. color: white;
  78. }
  79. .add-cart-text {
  80. font-size: 20px;
  81. text-align: center;
  82. width: 300px;
  83. height: 50px;
  84. background-color: #ffd700;
  85. color: white;
  86. }

構(gòu)建頁面邏輯

開發(fā)者在 index.js 文件中構(gòu)建頁面邏輯,主要實現(xiàn)的是兩個邏輯功能:

  • 當(dāng)焦點移動到不同顏色的圓形,swiper 滑動到不同的圖片;
  • 當(dāng)焦點移動到購物車區(qū)時,“Add To Cart”背景顏色從淺藍(lán)變成深藍(lán),點擊后文字變化為“Cart + 1”,背景顏色由深藍(lán)色變成黃色,添加購物車不可重復(fù)操作。

邏輯頁面代碼示例如下:

  1. // index.js
  2. export default {
  3. data: {
  4. cartText: 'Add To Cart',
  5. cartStyle: 'cart-text',
  6. isCartEmpty: true,
  7. descriptionFirstParagraph: 'This is the food page including fresh fruit, meat, snack and etc. You can pick whatever you like and add it to your Cart. Your order will arrive within 48 hours. We gurantee that our food is organic and healthy. Feel free to ask our 24h online service to explore more about our platform and products.',
  8. imageList: ['/common/food_000.JPG', '/common/food_001.JPG', '/common/food_002.JPG', '/common/food_003.JPG'],
  9. canvasList: [{
  10. id: 'cycle0',
  11. index: 0,
  12. color: '#f0b400',
  13. }, {
  14. id: 'cycle1',
  15. index: 1,
  16. color: '#e86063',
  17. }, {
  18. id: 'cycle2',
  19. index: 2,
  20. color: '#597a43',
  21. }, {
  22. id: 'cycle3',
  23. index: 3,
  24. color: '#e97d4c',
  25. }],
  26. },
  27. onShow() {
  28. this.canvasList.forEach(element => {
  29. this.drawCycle(element.id, element.color);
  30. });
  31. },
  32. swipeToIndex(index) {
  33. this.$element('swiperImage').swipeTo({index: index});
  34. },
  35. drawCycle(id, color) {
  36. var greenCycle = this.$element(id);
  37. var ctx = greenCycle.getContext("2d");
  38. ctx.strokeStyle = color;
  39. ctx.fillStyle = color;
  40. ctx.beginPath();
  41. ctx.arc(15, 25, 10, 0, 2 * 3.14);
  42. ctx.closePath();
  43. ctx.stroke();
  44. ctx.fill();
  45. },
  46. addCart() {
  47. if (this.isCartEmpty) {
  48. this.cartText = 'Cart + 1';
  49. this.cartStyle = 'add-cart-text';
  50. this.isCartEmpty = false;
  51. }
  52. },
  53. getFocus() {
  54. if (this.isCartEmpty) {
  55. this.cartStyle = 'cart-text-focus';
  56. }
  57. },
  58. lostFocus() {
  59. if (this.isCartEmpty) {
  60. this.cartStyle = 'cart-text';
  61. }
  62. },
  63. }

效果示例

實現(xiàn)此實例后,效果示例如下圖所示。

圖3 運行效果 點擊放大

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號