vue.js 實(shí)現(xiàn)評(píng)價(jià)五角星組件的實(shí)例代碼
餓了么的五角星有三種形狀,分別是實(shí)星,半星,空星

并且組件要能實(shí)現(xiàn),這個(gè)五角星不同大小,評(píng)分也不一樣,比如滿分五顆星,四顆半星,四顆星等等....
所以需要像組件傳入一個(gè)大小:size,一個(gè)分?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(){
//計(jì)算屬性
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) {
//有且最多一個(gè)半星
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圖片

對(duì)了,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")
組件的使用就很簡(jiǎn)單了
<star :size="48" :score="3.5"></star>

總結(jié)
以上所述是小編給大家介紹的vue.js 實(shí)現(xiàn)評(píng)價(jià)五角星組件的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue2中組件互相調(diào)用實(shí)例methods中的方法實(shí)現(xiàn)詳解
vue在同一個(gè)組件內(nèi),方法之間經(jīng)常需要互相調(diào)用,下面這篇文章主要給大家介紹了關(guān)于vue2中組件互相調(diào)用實(shí)例methods中的方法實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
vue 全選與反選的實(shí)現(xiàn)方法(無(wú)Bug 新手看過(guò)來(lái))
下面小編就為大家分享一篇vue 全選與反選的實(shí)現(xiàn)方法(無(wú)Bug 新手看過(guò)來(lái)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Vuex state中同步數(shù)據(jù)和異步數(shù)據(jù)方式
這篇文章主要介紹了Vuex state中同步數(shù)據(jù)和異步數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
vue3的自定義指令directives實(shí)現(xiàn)
本文主要介紹了vue3的自定義指令directives實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
vue2.x 對(duì)象劫持的原理實(shí)現(xiàn)
這篇文章主要介紹了vue2.x 對(duì)象劫持的原理實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Vue3+antDesignVue實(shí)現(xiàn)表單校驗(yàn)的方法
這篇文章主要為大家詳細(xì)介紹了基于Vue3和antDesignVue實(shí)現(xiàn)表單校驗(yàn)的方法,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的小伙伴可以了解下2024-01-01

