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


Posted in Javascript onNovember 16, 2018

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

1.效果图

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

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

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

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

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

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

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

<!-- (size * stars.length + (size/2) * 4 + 20 )这里的话,是在可以点击的时候,加上了好评的字体的长度 -->
<view class='starsBox' style='width:{{isClick?(size * stars.length + (size/2) * 4 + 20 ):(size * stars.length)}}rpx;height:{{size}}rpx;'>
 <view class='stars' style='width:{{size * stars.length}}rpx;height:{{size}}rpx;'>
 <block wx:for="{{stars}}" wx:key="{{index}}">
  <image src="/images/{{item == 0 ? 'grayStar':item}}.png" style='width:{{size}}rpx;height:{{size}}rpx;' data-index="{{index}}" catchtap="computeScore"></image>
 </block>
 </view>
 <view wx:if="{{isClick}}" class='text' style='font-size:{{size/2}}rpx;'>
 <text wx:if="{{value=='0'}}" class='pointText'>暂无评分</text>
 <text wx:elif="{{value=='1'}}" class='pointText'>差评</text>
 <text wx:elif="{{value<'4'}}" class='pointText'>中评</text>
 <text wx:else class='pointText'>好评</text>
 </view>
</view>

子组件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,
  /* 监听value值的变化 */
  observer: function (newVal, oldVal, changedPath) {
  this.init()
  }
 },
 /* 设置星星大小 */
 size: {
  type: Number,
  value: 30
 },
 /* 是否可点击,type为null表示值可以是任意类型 */
 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

<view class="score">
 <view class="scoreItem">
  <score value="{{shopGrade}}" size="46" isClick="true" bindcompute="computeGrade" />
 </view>
 <view class="scoreItem">
  <score value="{{shopGrade1}}" size="46" /> 
 </view>
</view>

父组件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

/* 在头部添加 */
<script type="text/javascript">
   document.getElementsByTagName("html")[0].style.fontSize = 100 / 750 * window.screen.width + "px";
  </script>

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

<template>
  <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position:absolute;width:0;height:0;visibility:hidden">
    <defs>
      <symbol id="star" viewBox="0 0 32 32">
        <path class="path1" d="M16 26.382l-8.16 4.992c-1.5 0.918-2.382 0.264-1.975-1.435l2.226-9.303-7.269-6.218c-1.337-1.143-0.987-2.184 0.755-2.322l9.536-0.758 3.667-8.835c0.674-1.624 1.772-1.613 2.442 0l3.667 8.835 9.536 0.758c1.753 0.139 2.082 1.187 0.755 2.322l-7.269 6.218 2.226 9.303c0.409 1.71-0.485 2.347-1.975 1.435l-8.16-4.992z">
        </path>
      </symbol>
    </defs>
  </svg>
</template>
<script></script>
<style></style>

rating.vue文件引用svg.vue

<template>
  <div class="ratingstar">
    <section class="star_container">
      <svg class="grey_fill" v-for="(num,index) in 5" :key="index">
        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#star" rel="external nofollow" rel="external nofollow" ></use>
      </svg>
    </section>
    <div class="star_overflow" :style="'width:'+rating*2/10+'rem'">
      <section class="star_container">
        <svg class="orange_fill" v-for="(num,index) in 5" :key="index">
          <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#star" rel="external nofollow" rel="external nofollow" ></use>
        </svg>
      </section>
    </div>
    <svgIcon></svgIcon>
  </div>
</template>

<script>
 import svgIcon from '@/components/svg'
 export default {
  components: {
   svgIcon
  },
  data() {
    return {
      rating: 4.2
    }
  },
 }
</script>
<style lang="less" rel="stylesheet/less" scoped>
  .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;
      }
    }
  }
</style>

总结

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

Javascript 相关文章推荐
js 函数的执行环境和作用域链的深入解析
Nov 01 Javascript
Node.js事件驱动
Jun 18 Javascript
jQuery Dialog 打开时自动聚焦的解决方法(两种方法)
Nov 24 Javascript
jQuery图片轮播实现并封装(一)
Dec 03 Javascript
Vue 2.x教程之基础API
Mar 06 Javascript
jQuery自定义多选下拉框效果
Jun 19 jQuery
vue.js数据绑定的方法(单向、双向和一次性绑定)
Jul 13 Javascript
Angularjs实现上传图片预览功能
Sep 01 Javascript
vue如何通过id从列表页跳转到对应的详情页
May 01 Javascript
深入浅析nuxt.js基于ssh的vue通用框架
May 21 Javascript
Layui弹出层 加载 做编辑页面的方法
Sep 16 Javascript
Vue通过for循环随机生成不同的颜色或随机数的实例
Nov 09 Javascript
layui-laydate时间日历控件使用方法详解
Nov 15 #Javascript
laydate如何根据开始时间或者结束时间限制范围
Nov 15 #Javascript
layDate插件设置开始和结束时间
Nov 15 #Javascript
解决vue2 在mounted函数无法获取prop中的变量问题
Nov 15 #Javascript
vue中使用better-scroll实现滑动效果及注意事项
Nov 15 #Javascript
layDate日期控件使用方法详解
Nov 15 #Javascript
vue2中引用及使用 better-scroll的方法详解
Nov 15 #Javascript
You might like
php递归实现无限分类的方法
2015/07/28 PHP
php版微信自定义回复功能示例
2016/12/05 PHP
使用按钮控制以何种方式打开新窗口的属性介绍
2012/12/17 Javascript
JS中Iframe之间传值的方法
2013/03/11 Javascript
avascript中的自执行匿名函数应用示例
2014/09/15 Javascript
JavaScript中数组继承的简单示例
2015/07/29 Javascript
浅谈Angular中ngModel的$render
2016/10/24 Javascript
给easyui的datebox控件添加清空按钮的实现方法
2016/11/09 Javascript
微信小程序 网络API Websocket详解
2016/11/09 Javascript
jquery对象与DOM对象转化
2017/02/08 Javascript
iview Upload组件多个文件上传的示例代码
2018/09/30 Javascript
微信小程序自定义组件传值 页面和组件相互传数据操作示例
2019/05/05 Javascript
jquery实现掷骰子小游戏
2019/10/24 jQuery
vue 点击其他区域关闭自定义div操作
2020/07/17 Javascript
[01:24:34]2014 DOTA2华西杯精英邀请赛5 24 DK VS LGD
2014/05/25 DOTA
python二叉树的实现实例
2013/11/21 Python
python获取本机外网ip的方法
2015/04/15 Python
python在指定目录下查找gif文件的方法
2015/05/04 Python
python 读取excel文件生成sql文件实例详解
2017/05/12 Python
python使用Tkinter实现在线音乐播放器
2018/01/30 Python
Django中Model的使用方法教程
2018/03/07 Python
caffe binaryproto 与 npy相互转换的实例讲解
2018/07/09 Python
Python利用递归实现文件的复制方法
2018/10/27 Python
Python 3 判断2个字典相同
2019/08/06 Python
Python Flask框架实现简单加法工具过程解析
2020/06/03 Python
Python加速程序运行的方法
2020/07/29 Python
Python try except else使用详解
2021/01/12 Python
html5启动原生APP总结
2020/07/03 HTML / CSS
互动出版网:专业书籍
2017/03/21 全球购物
Holland & Barrett爱尔兰:英国领先的健康零售商
2019/03/31 全球购物
旅游安全协议书
2014/04/21 职场文书
毕业大学生自荐信
2014/06/17 职场文书
2014年技术工作总结范文
2014/11/20 职场文书
初中历史教学反思
2016/02/19 职场文书
作文之亲情600字
2019/09/23 职场文书
SQL中去除重复数据的几种方法汇总(窗口函数对数据去重)
2023/05/08 MySQL