vue实现星级评价效果
希望對你們有用,已經(jīng)自己試過可以的才發(fā)布出來的
效果如下:
html:
<template>
<div class="evaStar">
<ul class="star">
<li v-for="(itemClass,index) in itemClasses" :class="itemClass" class="star-item" @click="stars(index)" track-by="index"></li><!--性能優(yōu)化 track-by 數(shù)據(jù)不改變時不會重新渲染-->
</ul>
</div>
</template>
?
JS:
<script>
export default{
data(){
return{
score: 4,
}
},
computed:{ //計算屬性
itemClasses(){
let result = []; // 返回的是一個數(shù)組,用來遍歷輸出星星
let score = Math.floor(this.score * 2 ) / 2; // 計算所有星星的數(shù)量
let hasDecimal = score % 1 !== 0; // 非整數(shù)星星判斷
let integer = Math.floor(score); // 整數(shù)星星判斷
for(let i=0;i<integer;i++){ // 整數(shù)星星使用on
result.push("on"); // 一個整數(shù)星星就push一個CLS_ON到數(shù)組
}
if(hasDecimal){ // 非整數(shù)星星使用half
result.push("half"); // 類似
}
while(result.length < 5){// 余下的用無星星補全,使用off
result.push("off");
}
return result;
}
},
methods:{
stars:function(index){
this.score = index + 1
}
}
}
</script>
?
CSS:
<style>
.evaStar{
float: right;
padding-top: .34rem;
}
.star{
font-size: 0;
}
.star-item{
display: inline-block;
background-repeat: no-repeat;
width: .6rem;
height: .6rem;
margin-left: .27rem;
background-size: 100%;
}
.star-item.on{
background-image: url(../assets/images/on.png);
}
.star-item.half{
background-image: url(../assets/images/half.png);
}
.star-item.off{
background-image: url(../assets/images/off.png);
}
</style>
轉(zhuǎn)載于:https://www.cnblogs.com/mei1234/p/8205640.html
總結(jié)
以上是生活随笔為你收集整理的vue实现星级评价效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 邮储银行信用卡激活有时间限制吗?有用卡需
- 下一篇: html5倒计时秒杀怎么做,vue 设