当前位置:   article > 正文

harmonyos的Js UI框架开发一个JS FA应用_鸿蒙js ui框架购物车

鸿蒙js ui框架购物车

开发一个JS FA应用

此应用相对于Hello World应用模板具备更复杂的页面布局、页面样式和页面逻辑。该页面可以通过将焦点移动到不同颜色的圆形来选择不同的食物图片,也可以进行添加到购物车操作,应用效果图如下。

图1 JS FA应用效果图
 

0000000000011111111.20200909202814.81732518949346005005165762226011:50510909131851:2800:FECDD0ABDC310D6673AAC7518AD7DE43372C3788321270F96A108C6CA778AD2D.png?needInitFileName=true?needInitFileName=true?needInitFileName=true

构建页面布局

开发者在index.hml文件中构建页面布局。在进行代码开发之前,首先要对页面布局进行分析,将页面分解为不同的部分,用容器组件来承载。根据JS FA应用效果图,此页面一共分成三个部分:标题区、展示区和购物车区。根据此分区,可以确定根节点的子节点应按列排列。

标题区是由两个按列排列的text组件实现,购物车区由一个text组件构成。展示区由按行排列的swiper组件和div组件组成,如下图所示:

  • 第一部分是由一个容器组件swiper,包含了四个image组件构成;
  • 第二部分是由一个容器组件div,包含了一个text组件和四个画布组件canvas绘制的圆形构成。

图2 展示区布局
 

0000000000011111111.20200909202814.29254414479764008155526557641761:50510909131851:2800:951CEB3D05757DDB192717D417F690CA99BE35B0129237A379503F90EA674395.png?needInitFileName=true?needInitFileName=true?needInitFileName=true

根据布局结构的分析,实现页面基础布局的代码示例如下(其中四个image组件和canvas组件通过for指令来循环创建):

  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目录下。

构建页面样式

开发者在index.css文件中需要设定的样式主要有flex-direction(主轴方向),padding(内边距),font-size(字体大小)等。在构建页面样式中,还采用了css伪类的写法,当焦点移动到canvas组件上时,背景颜色变成白色,也可以在js中通过focus和blur事件动态修改css样式来实现同样的效果。

  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. }

构建页面逻辑

开发者在index.js文件中构建页面逻辑,主要实现的是两个逻辑功能:

  • 当焦点移动到不同颜色的圆形,swiper滑动到不同的图片;
  • 当焦点移动到购物车区时,“Add To Cart”背景颜色从浅蓝变成深蓝,点击后文字变化为“Cart + 1”,背景颜色由深蓝色变成黄色,添加购物车不可重复操作。

逻辑页面代码示例如下:

  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. }

效果示例

实现此实例后,效果示例如下图所示。

图3 运行效果
 

0000000000011111111.20200909202814.39982866543385972816499258664641:50510909131851:2800:8AE94C89A289E5844AE6BB41A11F8E532B3033E3C0DB40443704F20589E7813A.gif?needInitFileName=true?needInitFileName=true?needInitFileName=true

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/296960
推荐阅读
相关标签
  

闽ICP备14008679号