star评星 点亮星星 原生js评星 vue评星

10 篇文章 0 订阅

类似于淘宝订单交易成功后,会进行评价。

项目结构如下:

一:原生js进行评星

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8" />

<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>

<title>评星</title>

<style>

.star {

width: 30px;

}

</style>

</head>



<body>

<div>

<p>请评价:</p>

<div class="star-warp">

<img src="./img/star.png" class="star" />

<img src="./img/star.png" class="star" />

<img src="./img/star.png" class="star" />

<img src="./img/star.png" class="star" />

<img src="./img/star.png" class="star" />

</div>

</div>

<script>

var star =document.getElementsByClassName("star");//获取所有的img

var sureIndex = 1;//已经确定的星星的索引 第一次评星的时候 此值是-1 0表示选择了1个星星

lightStar(sureIndex);//初始化数据

for(var i = 0;i<star.length;i++){

(function(i){

//mouseover

star[i].onmouseover =function(){

lightStar(i);

};

//mouseout

star[i].onmouseout =function(){

lightStar(sureIndex);

};

//点击

star[i].onclick =function(){

sureIndex = i;

lightStar(i);

};

}(i))

}



//点亮星星

function lightStar(count){

for(var i = 0;i<=count;i++){

star[i].src='./img/star1.png';

}

for(var i = (count+1);i<5;i++){

star[i].src='./img/star.png';

}

}

</script>

</body>

</html>

原生js主要是根据index来进行相应的样式变化,重点是弄清楚索引,,如图,黄色星星的索引分别是0,1,灰色索引是以(0+黄色星星个数开始的),即灰色星星索引以2开始。

2.vue写法,依赖vue.js

<!DOCTYPE html>

<html lang="en">



<head>

<meta charset="UTF-8" />

<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />

<title>评星</title>

<style>

.star {

width: 30px;

}

</style>

</head>



<body>

<div id="content">

<p>请评价:</p>

<div class="star-warp">

<img src="./img/star1.png" class="star" v-for="(item,index) in (hoveIndex+1)" @mouseover="overStar(index)"

@mouseout="outStar(index)" @click="clickStar(index)" />

<img src="./img/star.png" class="star" v-for="(item,index) in (4-hoveIndex)" @mouseover="overStar(index+hoveIndex+1)"

@mouseout="outStar(index+hoveIndex+1)" @click="clickStar(index+hoveIndex+1)" />

</div>

</div>

<script src="./js/vue.js"></script>

<script>

var vm = new Vue({

el: '#content',

data: {

sureIndex: 1, //已经确定的星星的索引 第一次评星的时候 此值是-1 0表示有1个星星确定

hoveIndex: 1 //hover星星的索引 第一次评星的时候 此值是-1与surIndex一致

},

methods: {

//mouseover

overStar: function (index) {

if (index >= 4) { //避免鼠标滑快

index = 4;

}

this.hoveIndex = index;

},

//mouseout

outStar: function (index) {

if (index >= 4) { //避免鼠标滑快

index = 4;

}

this.hoveIndex = this.sureIndex;

},

//点击星星

clickStar: function (index) {

this.hoveIndex = this.sureIndex = index;

}

}

})

</script>

</body>



</html>

使用Vue的时候,同原生js一样,重点是索引的值,另外,此次添加了个变量hoverIndex,来区别mouseover和mouseout显示的星星。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 Vue 星级评分组件的实现: ```html <template> <div class="rating"> <span v-for="(star, index) in stars" :class="['star', star]" :key="index" @click="select(index)"> </span> </div> </template> <script> export default { props: { value: { type: Number, default: 0 }, max: { type: Number, default: 5 } }, data() { return { stars: [] } }, mounted() { this.updateStars() }, methods: { updateStars() { this.stars = [] for (let i = 0; i < this.max; i++) { this.stars.push(i < this.value) } }, select(index) { this.value = index + 1 this.$emit('input', this.value) this.updateStars() } } } </script> <style> .star { display: inline-block; width: 24px; height: 24px; background: url(star.png) no-repeat; background-size: contain; cursor: pointer; } .star:hover, .star.active { background-position: 0 -24px; } </style> ``` 使用方式: ```html <template> <div> <h3>我的评分:{{ rating }}</h3> <star-rating v-model="rating"></star-rating> </div> </template> <script> import StarRating from './StarRating.vue' export default { components: { StarRating }, data() { return { rating: 3 } } } </script> ``` 其中,star.png 是一张包含两个星星的图片,第一个星星是默认状态,第二个星星是激活状态。 这个组件的实现很简单,通过 props 接收评分值和最大值,然后用一个数组来表示星星的激活状态,点击某个星星时更新评分值和星星状态,通过事件向父组件传递更新后的评分值。最后,用 CSS 来实现星星样式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值