這篇文章主要介紹了vue.js如何實現(xiàn)評價五角星組件,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)公司"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機(jī)版的企業(yè)網(wǎng)站。實現(xiàn)跨屏營銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營銷需求!成都創(chuàng)新互聯(lián)公司具備承接各種類型的成都網(wǎng)站建設(shè)、成都做網(wǎng)站項目的能力。經(jīng)過十年的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評。
餓了么的五角星有三種形狀,分別是實星,半星,空星

并且組件要能實現(xiàn),這個五角星不同大小,評分也不一樣,比如滿分五顆星,四顆半星,四顆星等等....
所以需要像組件傳入一個大小:size,一個分?jǐn)?shù):score
代碼如下:
<template>
<div class="star" :class="starType">
<span class="star-item" :class="itemClass" v-for="itemClass in itemClasses"></span>
</div>
</template>
<script type="text/ecmascript-6">
const LENGTH=5;
const CLS_ON="on";
const CLS_OFF="off";
const CLS_HALF="half";
export default {
props:{
size:{
type:Number
},
score:{
type:Number
}
},
computed:{
starType(){
return "star-"+this.size;
},
itemClasses(){
//計算屬性
let result=[];
let score=Math.floor(this.score*2)/2;
let hasDecimal=score%1!==0; //是否有效數(shù)
let integer=Math.floor(score);//取整
for (var i = 0; i < integer; i++) {
result.push(CLS_ON);
}
if (hasDecimal) {
//有且最多一個半星
result.push(CLS_HALF);
}
while(result.length<LENGTH){
result.push(CLS_OFF);
}
//數(shù)組填充完畢
return result;
}
}
};
</script>
<style lang="stylus" ref="sheetstyle/stylus">
@import "../../common/stylus/mixin.styl";
.star
font-size:0
.star-item
display:inline-block
background-repeat:no-repeat
&.star-48
.star-item
width:20px
height:20px
margin-right:22px
background-size:20px 20px
&:last-child
margin-right:0
&.on
bg-image('star48_on')
&.half
bg-image('star48_half')
&.off
bg-image('star48_off')
&.star-36
.star-item
width:15px
height:15px
margin-right:6px
background-size:15px 15px
&:last-child
margin-right:0
&.on
bg-image('star36_on')
&.half
bg-image('star36_half')
&.off
bg-image('star36_off')
&.star-24
.star-item
width:10px
height:10px
margin-right:3px
background-size:10px 10px
&:last-child
margin-right:0
&.on
bg-image('star24_on')
&.half
bg-image('star24_half')
&.off
bg-image('star24_off')
</style>其中:使用了size為48,36,24,所以我們需要圖片3種類型的圖片,并且要使用適應(yīng)不同分辨率,要有@2x.png,@3x.png圖片

對了,bg-image方法是在mixin.styl中的,代碼如下
bg-image($url) background-image:url($url+"@2x.png") @media (-webkit-min-device-pixel-ratio:3),(min-device-pixel-ratio:3) background-image:url($url+"@3x.png")
組件的使用就很簡單了
<star :size="48" :score="3.5"></star>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“vue.js如何實現(xiàn)評價五角星組件”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
本文名稱:vue.js如何實現(xiàn)評價五角星組件
URL分享:http://www.chinadenli.net/article16/gpdggg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、定制開發(fā)、網(wǎng)站維護(hù)、標(biāo)簽優(yōu)化、服務(wù)器托管、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)