Vue星级评价效果

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Vue星级评价效果

조국화   2022-05-23 我要评论

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>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们