vue星級(jí)評(píng)分組件源碼,具體代碼如下所示:
<!--自定義組件--><template> <!--結(jié)構(gòu)層--> <div id="star"> <span v-for="classn in starArrs" :class="classn" class="staritem"></span> <!--<span class="staritem onstar"></span> <span class="staritem onstar"></span> <span class="staritem halfstar"></span> <span class="staritem offstar"></span>--> <!--根據(jù)傳遞過來的分?jǐn)?shù)得到對(duì)應(yīng)的星星 4.7 四顆半星 1個(gè)位數(shù)代表的是全里亮, 2,當(dāng)分?jǐn)?shù)大于等于0.5 是一個(gè)半星 3.當(dāng)全星和半星不足五個(gè)的時(shí)候,剩下的都是灰色的星星 --> </div></template><script>// 行為 export default{ props:['score'], created(){ console.log("子組件"); }, mounted(){ console.log("打印傳遞的分?jǐn)?shù)"); }, computed:{ starArrs(){ var starArr=[]; //3.9// 全星星的個(gè)數(shù) let onstar = parseInt(this.score); console.log(onstar)// 是否有半星 let halfStar = Math.round(this.score-onstar)?true:false;// 有多少課灰色的星星 for (var i=0;i<onstar;i++) { starArr.push('onstar') } if(halfStar){ starArr.push('halfstar') } while(starArr.length<5){ starArr.push('offstar') } return starArr; } } }</script><style scoped="scoped"> /*獨(dú)立作用域的樣式*/ .staritem{ display: inline-block; width: 0.37037rem; height: 0.37037rem; } /*全星星*/ .onstar{ background: url(./star24_on@2x.png); background-size: 0.37037rem; } .halfstar{ background: url('./star24_half@2x.png'); } .offstar{ background: url('./star24_off@2x.png'); }</style>背景圖



總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)簡(jiǎn)單的星級(jí)評(píng)分組件源碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注