Vue implements star rating with decimal points

Vue implements star rating with decimal points

This article shares the specific code of Vue to implement star rating with decimal points for your reference. The specific content is as follows

First we need to import the vue.js file

CSS part

<style>
 main{
  position:relative;
  }
 .star_line{
  /* Set to force no line break */
  width-space:nowrap;
  overflow: hidden;
  position: absolute;
  }
 .star{
  display: inline-block;
  /* Set the mouse to turn into a small hand style when it is placed on a star*/
  cursor: pointer
  }
</style>

Body

<div id="app">
 <input type="text" v-model.number="score">
 <- When any component receives the bound value in a two-way binding, it must use value to receive it. The principle is as follows: input ->
 <v-star v-model="score"></v-star>
</div>

In the js part, we use components, input is in the root component, and the stars we created are placed in a component. The star rating is mainly realized through two-way binding, where the parent component and the child component pass values ​​to each other.

Component template part

<script id="v-star" type="text/html">
    <main :style="mainStyle">
        <!-- White Star -->
        <div class="star_line">
            <span @click="changeValue(star)" class="star" :style="starStyle" v-for="star in total">☆</span>
        </div>
        <!-- Black Star -->
        <div class="star_line" :style="blackStyle">
            <span @click="changeValue(star-1)" class="star" :style="starStyle" v-for="star in total">★</span>
        </div>
    </main>
</script>

js part

<script>
    Vue.component("v-star",{
        template:"#v-star",
        props:{
            total:{
                default:10,
            },
            size:{
                default:30
            },
            // Receive the score passed from the parent component
            value:{}
        },
        // Computed properties computed:{
            mainStyle(){
                return {
                    width:this.size * this.total + "px",
                }
            },
            starStyle(){
                return {
                    width:this.size + "px",
                    height:this.size + "px",
                    fontSize: this.size + 6 + "px"
                }
            },
            blackStyle(){
                return {
                    width:this.value / this.total * 100 + "%"
                }
            }
        },
        methods:{
            changeValue(value){
                // Pass the latest result to input
                // The input tag has a default input event this.$emit("input",value)
            }
        }
    })

    new Vue({
        el:"#app",
        data:{
            score:1
        }
    })
</script>

Rendering

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue2.0 implements the front-end star rating function component example code

<<:  CentOS 7 Forgot Password Solution Process Diagram

>>:  Troubleshooting of master-slave delay issues when upgrading MySQL 5.6 to 5.7

Recommend

A brief discussion on MySQL user permission table

MySQL will automatically create a database named ...

A simple way to call desktop exe programs on a web page

This article mainly introduces how to call desktop...

Configure VIM as a C++ development editor in Ubuntu

1. Copy the configuration file to the user enviro...

React+Antd implements an example of adding, deleting and modifying tables

Table of contents Table/index.js Table/model/inde...

Vue custom encapsulated button component

The custom encapsulation code of the vue button c...

How to install JDK and Mysql on Ubuntu 18.04 Linux system

Platform deployment 1. Install JDK step1. Downloa...

Implementation of element input box automatically getting focus

When making a form in a recent project, I need to...

Two ways to specify the character set of the html page

1. Two ways to specify the character set of the h...

Pure CSS3 to create page switching effect example code

The one I wrote before is too complicated, let’s ...

Vue uses drag and drop to create a structure tree

This article example shares the specific code of ...

Detailed explanation of the update command for software (library) under Linux

When installing packages on an Ubuntu server, you...

Linux system to view CPU, machine model, memory and other information

During system maintenance, you may need to check ...