js評分組件使用詳解
我們知道,許多外賣app都有評分的星星,這里我總結(jié)一下對評分組件的開發(fā),學(xué)習(xí)視頻:餓了么實(shí)戰(zhàn)(慕課網(wǎng))

1.html部分
<div class="star" :class="starType">
<span v-for="itemClass in itemClasses" :class="itemClass" class="star-item" track-by="$index"></span>
</div>
解釋
1.在大的div里綁定starType是因?yàn)樵谡麄€(gè)App中,有多個(gè)評分組件,而它們的大小不一樣,所以根據(jù)大小動(dòng)態(tài)的綁定class.
同樣的原理,在上一節(jié)header組件開發(fā)中也有介紹,但直到寫到這里我開始漸漸明白vue.js中:class的意義。以前我想既然可以直接添加class,為什么要用綁定class來多此一舉?,F(xiàn)在我明白的,基礎(chǔ)的樣式設(shè)定,直接添加class就可以了,但是有時(shí)候涉及到根據(jù)不同的狀態(tài)有不同的樣式時(shí),就要用綁定class了。
2.v-for 這里我們沒有寫5個(gè)span,而是遍歷itemClasses,這是vue.js中的一種常用方法。既減少了代碼,而且動(dòng)態(tài)獲取數(shù)據(jù)。
2.js部分
1. 得到評分?jǐn)?shù)據(jù)
像上一節(jié)一樣,我們通過props來接收數(shù)據(jù)。我們要接收的是兩個(gè)number類型的數(shù)據(jù),一個(gè)是星星的尺寸,一個(gè)是分?jǐn)?shù)。
props: {
size:{
type:Number
},
score:{
type:Number
}
}
2.屬性的計(jì)算
1).接收size動(dòng)態(tài)綁定不同的class
starType() {
return 'star-'+this.size;
}
.star-48 {
width: 20px;
height: 20px;
margin-right: 22px;
background-size: 20px 20px;
}
.star-36 {
width: 15px;
height: 15px;
margin-right: 6px;
background-size: 15px 15px;
}
.star-24 {
width: 10px;
height: 10px;
margin-right: 3px;
background-size: 10px 10px;
}
2). 通過計(jì)算確定添加全星半星和無星
const LENGTH = 5;
const CLS_ON = 'on';
const CLS_HALF = 'half';
const CLS_OFF = 'off';
itemClasses() {
let result = [];
let score = Math.floor(this.score*2)/2;
let hasDecimal = score%1 !== 0;
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);
}
return result;
}
這段代碼的思路是:創(chuàng)建一個(gè)數(shù)組儲(chǔ)存星星,判斷分?jǐn)?shù)是否在.5以上,將分?jǐn)?shù)取整,有多少分push幾個(gè)on星星進(jìn)去,有.5以上,push一個(gè)half,然后push進(jìn)off直到長度達(dá)到5。
3).css部分
以star-48的尺寸為例
.star-48 .on {
background-image: url('star48_on@2x.png')
}
.star-48 .half {
background-image: url('star48_half@2x.png')
}
.star-48 .off {
background-image: url('star48_off@2x.png')
}
4.完整代碼
<template>
<div class="star" :class="starType">
<span v-for="itemClass in itemClasses" :class="itemClass" class="star-item" track-by="$index"></span>
</div>
</template>
<script type="text/javascript">
const LENGTH = 5;
const CLS_ON = 'on';
const CLS_HALF = 'half';
const CLS_OFF = 'off';
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;
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);
}
return result;
}
}
};
</script>
<style type="text/css">
.star {
font-size: 0;
}
/* .star-48 {
width: 20px;
height: 20px;
margin-right: 22px;
background-size: 20px 20px;
} */
.star-48 : last-chlid {
margin-right: 0;
}
.star-48 .on {
background-image: url('star48_on@2x.png')
}
.star-48 .half {
background-image: url('star48_half@2x.png')
}
.star-48 .off {
background-image: url('star48_off@2x.png')
}
.star-36 {
width: 15px;
height: 15px;
margin-right: 6px;
background-size: 15px 15px;
}
.star-36 .no {
background-image: url('star36_on@2x.png')
}
.star-36 .half {
background-image: url('star36_half@2x.png')
}
.star-36 .off {
background-image: url('star36_off@2x.png')
}
.star-24 {
width: 10px;
height: 10px;
margin-right: 3px;
background-size: 10px 10px;
}
.star-24 .no {
background-image: url('star24_on@2x.png')
}
.star-24 .half {
background-image: url('star24_half@2x.png')
}
.star-24 .off {
background-image: url('star24_off@2x.png')
}
.star-item {
display: inline-block;
background-repeat: no-repeat;
width: 20px;
height: 20px;
margin-right: 22px;
background-size: 20px 20px;
}
</style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ES6 新增的創(chuàng)建數(shù)組的方法(小結(jié))
這篇文章主要介紹了ES6 新增的創(chuàng)建數(shù)組的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
js console.log打印對像與數(shù)組用法詳解
這篇文章主要介紹了js console.log打印對像與數(shù)組用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了js使用console.log實(shí)現(xiàn)打印對象與數(shù)組的具體實(shí)現(xiàn)步驟與相關(guān)技巧,需要的朋友可以參考下2016-01-01
微信小程序?qū)崿F(xiàn)全局狀態(tài)管理的方法整理
已知微信小程序中如果要做到全局狀態(tài)共享,常用的有四種方式,分別是globalData、本地緩存、mobx-miniprogram和westore,本文將帶大家看看mobx-miniprogram是如何實(shí)現(xiàn)的小程序的全局狀態(tài)管理,需要的可以收藏一下2023-06-06
JavaScript實(shí)現(xiàn)單點(diǎn)登錄的示例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)單點(diǎn)登錄的示例,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下2020-09-09

