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:`
`
, 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>

你可能感兴趣的:(Vue实现星级评价)