js評分組件使用詳解
我們知道,許多外賣app都有評分的星星,這里我總結(jié)一下對評分組件的開發(fā),學(xué)習(xí)視頻:餓了么實戰(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是因為在整個App中,有多個評分組件,而它們的大小不一樣,所以根據(jù)大小動態(tài)的綁定class.
同樣的原理,在上一節(jié)header組件開發(fā)中也有介紹,但直到寫到這里我開始漸漸明白vue.js中:class的意義。以前我想既然可以直接添加class,為什么要用綁定class來多此一舉?,F(xiàn)在我明白的,基礎(chǔ)的樣式設(shè)定,直接添加class就可以了,但是有時候涉及到根據(jù)不同的狀態(tài)有不同的樣式時,就要用綁定class了。
2.v-for 這里我們沒有寫5個span,而是遍歷itemClasses,這是vue.js中的一種常用方法。既減少了代碼,而且動態(tài)獲取數(shù)據(jù)。
2.js部分
1. 得到評分?jǐn)?shù)據(jù)
像上一節(jié)一樣,我們通過props來接收數(shù)據(jù)。我們要接收的是兩個number類型的數(shù)據(jù),一個是星星的尺寸,一個是分?jǐn)?shù)。
props: { size:{ type:Number }, score:{ type:Number } }
2.屬性的計算
1).接收size動態(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). 通過計算確定添加全星半星和無星
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)建一個數(shù)組儲存星星,判斷分?jǐn)?shù)是否在.5以上,將分?jǐn)?shù)取整,有多少分push幾個on星星進去,有.5以上,push一個half,然后push進off直到長度達到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)文章
發(fā)一個數(shù)據(jù)過濾的代碼,很簡單,有用的著的拿去
發(fā)一個數(shù)據(jù)過濾的代碼,很簡單,有用的著的拿去...2007-02-02javascript獲取xml節(jié)點的最大值(實現(xiàn)代碼)
這篇文章主要介紹了利用javascript獲取xml節(jié)點的最大值。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12解析ScrollPic在ie8下只滾動一遍,然后變?yōu)榭瞻?ie6,ie7,chrome,firefox正常
解析ScrollPic在ie8下只滾動一遍,然后變?yōu)榭瞻?ie6,ie7,chrome,firefox都正常)2013-06-06JavaScript鼠標(biāo)移動事件以及實戰(zhàn)案例
在學(xué)習(xí)JS中我對鼠標(biāo)移動事件進行了一些總結(jié),需要的可以作參考,下面這篇文章主要給大家介紹了關(guān)于JavaScript鼠標(biāo)移動事件以及實戰(zhàn)案例的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-05-05js(JavaScript)實現(xiàn)TAB標(biāo)簽切換效果的簡單實例
本篇文章主要是對js(JavaScript)實現(xiàn)TAB標(biāo)簽切換效果的簡單實例進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02JS學(xué)習(xí)筆記之原型鏈和利用原型實現(xiàn)繼承詳解
這篇文章主要介紹了JS學(xué)習(xí)筆記之原型鏈和利用原型實現(xiàn)繼承,結(jié)合實例形式詳細分析了javascript原型鏈以及利用原型實現(xiàn)繼承的相關(guān)操作技巧與注意事項,需要的朋友可以參考下2019-05-05原生ajax處理json格式數(shù)據(jù)的實例代碼
這篇文章主要介紹了原生ajax處理json格式數(shù)據(jù)的實例代碼,需要的朋友可以參考下2016-12-12