Vue实现星级评价

1、本文五角星采用的阿里巴巴矢量图标库
2、数据内容可根据实际需求进行改动;
3、主要实现了鼠标滑动改变样式,鼠标离开样式消失,鼠标点击对应的五角星样式被改变及完成评价
4、本文章只是简单的实现了功能,仅做参考!

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>五星评价(Vue组件)</title>
	<!-- 引入阿里矢量图标 -->
	<link rel="stylesheet" href="./iconfont.css">
	<!-- 引入vue -->
	<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
	
	<style>
		.box{
			width:400px;
			height: 100px;
			border: 1px solid black;
			margin:0 auto;
		}
		.box .icon-wjxb{
			font-size:44px;
			line-height: 100px;
			color:grey;
			padding: 0 10px;
		}
		.box  .active{
			color:yellow;
		}
	</style>
</head>
<body>
	<!-- 星级评价板块 -->
	<div class="box">
		<star></star>
	</div>
</body>
<script>
	// 创建局部组件
	var star = {
		// 子模版
		template:`<div>
					<span class="iconfont icon-wjxb" v-for="n in 5" @mouseenter="change(n)" @mouseleave="unchange" :class="{'active':activeIndex>=n}" @click="fix(n)"></span>
				</div>`,
		data(){
			return {
				activeIndex:-1,  // 用来判断什么时候改变样式
				flag:false       // 用来验证用户是否点击过
			}
		},
		methods:{
			// 鼠标进入触发,接收参数
			change(n){
				// 判断用户是否点击过
				if(!this.flag){
					// 没有点击过,改变样式
					this.activeIndex = n;
				}
			},
			// 鼠标离开时触发
			unchange(){
				// 判断用户是否点击过
				if(!this.flag){
					// 没点击过,鼠标离开后样式消失
					this.activeIndex = -1;
				}
			},
			// 点击时触发,接收参数
			fix(n){
				// 赋值
				this.activeIndex = n;
				// 改变状态
				this.flag = true;
			}
		}
	}
	new Vue({
		// 挂载元素
		el:".box",
		// 注册组件
		components:{
			star:star
		}
	})	
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值