微信小程序中的店鋪評(píng)分組件及vue中用svg實(shí)現(xiàn)的評(píng)分顯示組件
在微信小程序中,有遇到要展示店鋪評(píng)分,或者是訂單完成后對(duì)商品進(jìn)行評(píng)價(jià),用到了星星展示,查了下,在微信中無(wú)法使用svg實(shí)現(xiàn)圖片,微信中只能將svg圖片轉(zhuǎn)成base64來(lái)顯示,所以是在vue中使用的svg來(lái)實(shí)現(xiàn)評(píng)分
1.效果圖
微信中的可以點(diǎn)擊及顯示,但是,顯示的話,在4.2分,4點(diǎn)多分的時(shí)候,顯示的是半顆星
vue中用的是svg實(shí)現(xiàn),所以用的是占比的形式,可以有一點(diǎn)點(diǎn)的星
2.微信實(shí)現(xiàn)店鋪評(píng)分顯示及商品評(píng)價(jià)星星展示
子組件index.wxml,可以動(dòng)態(tài)的控制星星的大小
<!-- (size * stars.length + (size/2) * 4 + 20 )這里的話,是在可以點(diǎn)擊的時(shí)候,加上了好評(píng)的字體的長(zhǎng)度 --> <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'>暫無(wú)評(píng)分</text> <text wx:elif="{{value=='1'}}" class='pointText'>差評(píng)</text> <text wx:elif="{{value<'4'}}" class='pointText'>中評(píng)</text> <text wx:else class='pointText'>好評(píng)</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: { /* 顯示有色星星的個(gè)數(shù) */ value: { type: Number, value: 0, /* 監(jiān)聽(tīng)value值的變化 */ observer: function (newVal, oldVal, changedPath) { this.init() } }, /* 設(shè)置星星大小 */ size: { type: Number, value: 30 }, /* 是否可點(diǎn)擊,type為null表示值可以是任意類型 */ isClick: { type: null, value: false } }, attached() { /* 組件生命周期函數(shù),在組件實(shí)例進(jìn)入頁(yè)面節(jié)點(diǎn)樹(shù)時(shí)執(zhí)行 */ this.init(); }, data: { stars: [0, 0, 0, 0, 0] }, methods: { init() { let star = this.properties.value; let stars = [0, 0, 0, 0, 0]; /* 圖片名稱,通過(guò)設(shè)置圖片名稱來(lái)動(dòng)態(tài)的改變圖片顯示 */ 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 }) }, /* 可點(diǎn)擊時(shí),用于計(jì)算分?jǐn)?shù) */ 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, }, /* 評(píng)分處理事件 */ computeGrade(e) { let score = e.detail.score; this.setData({ shopGrade: score }) },
4.vue中使用svg實(shí)現(xiàn)評(píng)分
首先在vue使用的index.html的模板文件中添加一個(gè)rem轉(zhuǎn)換算法,因?yàn)槲液竺嬗玫膯挝皇莚em
/* 在頭部添加 */ <script type="text/javascript"> document.getElementsByTagName("html")[0].style.fontSize = 100 / 750 * window.screen.width + "px"; </script>
然后添加svg.vue文件,這個(gè)svg文件可以自己找圖片生成,并設(shè)置對(duì)應(yīng)的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>
總結(jié)
以上所述是小編給大家介紹的微信小程序中的店鋪評(píng)分組件及vue中用svg實(shí)現(xiàn)的評(píng)分顯示組件,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 微信小程序?qū)崿F(xiàn)星級(jí)評(píng)分
- 微信小程序?qū)崿F(xiàn)星級(jí)評(píng)分和展示
- 微信小程序?qū)崿F(xiàn)星星評(píng)分效果
- 微信小程序動(dòng)態(tài)評(píng)分展示/五角星展示/半顆星展示/自定義長(zhǎng)度展示功能的實(shí)現(xiàn)
- 微信小程序?qū)崿F(xiàn)展示評(píng)分結(jié)果功能
- 微信小程序 五星評(píng)分的實(shí)現(xiàn)實(shí)例
- 微信小程序五星評(píng)分效果實(shí)現(xiàn)代碼
- 微信小程序 五星評(píng)分(包括半顆星評(píng)分)實(shí)例代碼
- 微信小程序使用template標(biāo)簽實(shí)現(xiàn)五星評(píng)分功能
- 微信小程序?qū)崿F(xiàn)星級(jí)評(píng)分與展示
相關(guān)文章
IE與FF下javascript獲取網(wǎng)頁(yè)及窗口大小的區(qū)別詳解
本篇文章主要是對(duì)IE與FF下javascript獲取網(wǎng)頁(yè)及窗口大小的區(qū)別進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01javascript 模擬JQuery的Ready方法實(shí)現(xiàn)并出現(xiàn)的問(wèn)題
今天在閱讀網(wǎng)上一些模擬Jq的ready方法時(shí),發(fā)現(xiàn)一些小細(xì)節(jié),就是網(wǎng)上的ready事件大部分都是在onload事件執(zhí)行后加載,而jquery確能在onload加載前。2009-12-12最簡(jiǎn)單純JavaScript實(shí)現(xiàn)Tab標(biāo)簽頁(yè)切換的方式(推薦)
這篇文章主要介紹了最簡(jiǎn)單純JavaScript實(shí)現(xiàn)Tab標(biāo)簽頁(yè)切換的方式(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07JavaScript中對(duì)象property的刪除方法介紹
這篇文章主要介紹了JavaScript中對(duì)象property的刪除方法介紹,本文主要是對(duì)delete操作符的介紹,需要的朋友可以參考下2014-12-12javascript 小數(shù)取整簡(jiǎn)單實(shí)現(xiàn)方式
這篇文章主要介紹了javascript 小數(shù)取整d的簡(jiǎn)單實(shí)現(xiàn)方式,需要的朋友可以參考下2014-05-05uniapp 獲取頁(yè)面來(lái)源及注意事項(xiàng)
這篇文章主要介紹了uniapp 獲取頁(yè)面來(lái)源及注意事項(xiàng),獲取當(dāng)前頁(yè)面棧的實(shí)例,以數(shù)組形式按棧的順序給出,數(shù)組中的元素為頁(yè)面實(shí)例,第一個(gè)元素為首頁(yè),最后一個(gè)元素為當(dāng)前頁(yè)面,感興趣的朋友參考本文實(shí)例代碼2024-03-03用javascript實(shí)現(xiàn)在小方框中瀏覽大圖的代碼
用javascript實(shí)現(xiàn)在小方框中瀏覽大圖的代碼...2007-08-08