HarmonyOS Developer之五星好评

rating

rating为评分条组件,用来实现用户使用感受的衡量标准条(如:五星好评)

在pages/index目录下的hml文件中创建一个rating组件:

HTML

html 复制代码
<!-- xxx.hml -->
<div class="container">
  <rating></rating>
</div>

CSS

css 复制代码
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
rating {
  width: 80%;
  height: 150px;
}

实现非5星好评

rating组件通过设置numstars和rating属性设置评分条的星级总数和当前评星数:

HTML:

html 复制代码
<!-- xxx.hml -->
<div class="container">
  <rating numstars="6" rating="5">
  </rating>
</div>
css 复制代码
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
rating {
  width: 80%;
  height: 150px;
}

自定义评分样式

rating组件通过star-background、star-foreground和star-secondary属性设置单个星级未选择、选中和选中的次级背景图片:

HTML

html 复制代码
<!-- xxx.hml -->
<div class="container">
  <div style="width: 500px;height: 500px;align-items: center;justify-content: center;flex-direction: column;;">
    <rating numstars="5" rating="1" class="myrating" style="width: {{ratewidth}}; height:{{rateheight}}; star-background: {{backstar}}; star-secondary: {{secstar}};star-foreground: {{forestar}};rtl-flip: true;">
    </rating>
  </div>
</div>

CSS

css 复制代码
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #F1F3F5;
}

JS

javascript 复制代码
// index.js
export default {
  data: {
    backstar: 'common/love.png',
    secstar: 'common/love.png',
    forestar: 'common/love1.png',
    ratewidth: '400px',
    rateheight: '150px'
  },
  onInit(){
  }
}
复制代码
说明
 - star-background、star-secondary、star-foreground属性的星级图源必须全部设置,否则默认的星级颜色为灰色,提示图源设置错误。
 - star-background、star-secondary、star-foreground属性只支持本地路径图片,图片格式为png和jpg。
相关推荐
HarmonyOS_SDK30 分钟前
汽车之家联合HarmonyOS SDK,深度构建鸿蒙生态体系
harmonyos
whysqwhw30 分钟前
鸿蒙沉浸式
harmonyos
whysqwhw2 小时前
鸿蒙Stack使用
harmonyos
whysqwhw2 小时前
鸿蒙Flex使用
harmonyos
whysqwhw2 小时前
鸿蒙Row/Column使用
harmonyos
一只栖枝10 小时前
华为 HCIE 大数据认证中 Linux 命令行的运用及价值
大数据·linux·运维·华为·华为认证·hcie·it
zhanshuo14 小时前
在鸿蒙里优雅地处理网络错误:从 Demo 到实战案例
harmonyos
zhanshuo14 小时前
在鸿蒙中实现深色/浅色模式切换:从原理到可运行 Demo
harmonyos
whysqwhw20 小时前
鸿蒙分布式投屏
harmonyos
whysqwhw21 小时前
鸿蒙AVSession Kit
harmonyos