栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

微信小程序中的店铺评分组件及vue中用svg实现的评分显示组件

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微信小程序中的店铺评分组件及vue中用svg实现的评分显示组件

在微信小程序中,有遇到要展示店铺评分,或者是订单完成后对商品进行评价,用到了星星展示,查了下,在微信中无法使用svg实现图片,微信中只能将svg图片转成base64来显示,所以是在vue中使用的svg来实现评分

1.效果图

微信中的可以点击及显示,但是,显示的话,在4.2分,4点多分的时候,显示的是半颗星

vue中用的是svg实现,所以用的是占比的形式,可以有一点点的星

2.微信实现店铺评分显示及商品评价星星展示

子组件index.wxml,可以动态的控制星星的大小



 
 
  
 
 
 
 暂无评分
 差评
 中评
 好评
 

子组件index.wxss

.starsBox{
 display: flex;
 align-items: center;
 justify-content: flex-start;
}
.stars{
 width: 150rpx;
 height: 50rpx;
 display: flex;
 align-items: center;
 justify-content: flex-start;
}
.stars image{
 width: 30rpx;
 height: 30rpx;
}
.text{
 color: #ccc;
 margin-left: 20rpx;
}

子组件index.js

Component({
 properties: {
 
 value: {
  type: Number,
  value: 0,
  
  observer: function (newVal, oldVal, changedPath) {
  this.init()
  }
 },
 
 size: {
  type: Number,
  value: 30
 },
 
 isClick: {
  type: null,
  value: false
 }
 },
 attached() {
 
 this.init();
 },
 data: {
 stars: [0, 0, 0, 0, 0]
 },
 methods: {
 init() {
  let star = this.properties.value;
  let stars = [0, 0, 0, 0, 0];
  
  for (let i = 0; i < Math.floor(star); i++) {
  stars[i] = 'star';
  }
  if (star > Math.floor(star)) {
  stars[Math.floor(star)] = 'halfStar';
  }
  for (let i = 0; i < stars.length; i++) {
  if (stars[i] == 0) {
   stars[i] = 'grayStar';
  }
  }
  this.setData({
  stars
  })
 },
 
 computeScore(e) {
  let index = e.currentTarget.dataset.index;
  let isClick = this.data.isClick;
  if (isClick) {
  let score = index + 1;
  this.triggerEvent('compute', {
   score
  });
  }
 }
 }
})

3.父组件中引用

父组件index.wxml


 
  
 
 
   
 

父组件index.json

{
 "usingComponents": {
  "score": "/component/score/index"
 }
}

父组件index.js

data: {
 shopGrade: 0,
 shopGrade1: 4.6,
},

computeGrade(e) {
 let score = e.detail.score;
 this.setData({
  shopGrade: score
 })
},

4.vue中使用svg实现评分

首先在vue使用的index.html的模板文件中添加一个rem转换算法,因为我后面用的单位是rem


然后添加svg.vue文件,这个svg文件可以自己找图片生成,并设置对应的id



rating.vue文件引用svg.vue





  .ratingstar {
    position: relative;
    width: 100%;
    .star_overflow {
      overflow: hidden;
      position: relative;
      height: 0.65rem;
    }
    .star_container {
      position: absolute;
      top: 0.05rem;
      width: 1rem;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .grey_fill {
 width: 0.94rem;
 height: 0.2rem;
 fill: #d1d1d1;
      }
      .orange_fill {
 width: 0.94rem;
 height: 0.2rem;
 fill: #ff9a0d;
      }
    }
  }

总结

以上所述是小编给大家介绍的微信小程序中的店铺评分组件及vue中用svg实现的评分显示组件,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/78234.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号