zoukankan      html  css  js  c++  java
  • 星星评分vue.js+onsen

    首先行引进onsen和vue.js包.

    onsen有自带css画的小星星这边只要引用就可以了

    <ons-page>
        <style>
            .ion-star{
                font-size: 24px;
                margin-right: 5px;
                color: #FFB400;
            }
            .ion-ios-star-outline{
                font-size: 24px;
                margin-right: 5px;
                color: black;
            }
            
        </style>    
        
         <div id="starIcon">
                <label>星星数量:{{starNum}}分</label>
                <template v-for="(star,index) in stars">
                         <ons-icon v-bind:icon="star.icon"   v-on:click="rating(index)"></ons-icon>
                </template>
         </div>
            
             <script>
                //星星的图片路径
                var starOffImg = "ion-ios-star-outline";
                var starOnImg = "ion-star";
    
                var app = new Vue({
                    el: "#starIcon",
                    data: {
                        stars: [{
                                icon: starOffImg,
                                active: false
                            }, {
                                icon: starOffImg,
                                active: false
                            }, {
                                icon: starOffImg,
                                active: false
                            },
                            {
                                icon: starOffImg,
                                active: false
                            }, {
                                icon: starOffImg,
                                active: false
                            }
                        ],
                        starNum: 0,
                    },
                    methods: {
                        //评分
                        rating: function(index) {
                            var total = this.stars.length; //星星总数
                            var idx = index + 1; //这代表选的第idx颗星-也代表应该显示的星星数量
    
                            //进入if说明页面为初始状态
                            if(this.starNum == 0) {
                                this.starNum = idx;
                                for(var i = 0; i < idx; i++) {
                                    this.stars[i].icon = starOnImg;
                                    this.stars[i].active = true;
                                }
                            } else {
                                //如果再次点击当前选中的星级-仅取消掉当前星级,保留之前的。
                                if(idx == this.starNum) {
                                    for(var i = index; i < total; i++) {
                                        this.stars[i].icon = starOffImg;
                                        this.stars[i].active = false;
                                    }
                                }
                                //如果小于当前最高星级,则直接保留当前星级
                                if(idx < this.starNum) {
                                    for(var i = idx; i < this.starNum; i++) {
                                        this.stars[i].icon = starOffImg;
                                        this.stars[i].active = false;
                                    }
                                }
                                //如果大于当前星级,则直接选到该星级
                                if(idx > this.starNum) {
                                    for(var i = 0; i < idx; i++) {
                                        this.stars[i].icon= starOnImg;
                                        this.stars[i].active = true;
                                    }
                                }
    
                                var count = 0; //计数器-统计当前有几颗星
                                for(var i = 0; i < total; i++) {
                                    if(this.stars[i].active) {
                                        count++;
                                    }
                                }
                                this.starNum = count;
                            }
                        }
                    }
                })
            </script>
        
    </ons-page>

    效果图:

  • 相关阅读:
    介绍几个程序员常去的网站
    你这辈子,为什么富不起来?!
    Bug解决:mysql 创建表字段Double类型长度
    RedisTemplate5种数据结构操作
    StringRedisTemplate与RedisTemplate区别
    Redis客户端信息的存取
    Anaconda安装pygame
    springboot启动报错
    idea上传项目到github
    Docker安装报错
  • 原文地址:https://www.cnblogs.com/wuxiaojuan/p/9529937.html
Copyright © 2011-2022 走看看