微信小程式之店鋪評分元件及vue中用svg實現的評分顯示元件
阿新 • • 發佈:2018-12-17
在微信小程式中,有遇到要展示店鋪評分,或者是訂單完成後對商品進行評價,用到了星星展示,查了下,在微信中無法使用svg實現圖片,微信中只能將svg圖片轉成base64來顯示,所以是在vue中使用的svg來實現評分
1.效果圖
微信中的可以點選及顯示,但是,顯示的話,在4.2分,4點多分的時候,顯示的是半顆星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"></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"></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>