1. 程式人生 > 程式設計 >vue實現帶小數點的星星評分

vue實現帶小數點的星星評分

本文例項為大家分享了實現帶小數點的星星評分的具體程式碼,供大家參考,具體內容如下

首先我們要先引入vue.檔案

部分

<style>
 main{
  position:relative;
  }
 .star_line{
  /*  設定強制不換行 */
  width-space: nowrap;
  overflow: hidden;
  position: absolute;
  }
 .star{
  display: inline-block;
  /* 設定當滑鼠放到星星上是變成小手樣式 */
  cursor: pointer
  }
</style>

body部分

<div id="app">
 <input type="text" v-model.number="score">
 <- 任何一個元件在進行雙向繫結接收繫結的值的時候,必須使用value來接收,原理參考input ->
 <v-star v-model="score"></v-star>
</div>

js部分我們用到元件,input在根元件內,而我們建立的星星放在一個元件內,主要通過雙向繫結,父元件和子元件相互傳值,來實現星星評分

元件模板部分

<script id="v-star" type="text/html">
    <main :style="mainStyle">
        <!-- 白星星 -->
        <div class="star_line">http://www.cppcns.com
<span @click="changeValue(star)" class="star" :style="starStyle" v-for="star in tGGNygDxotal">☆</span> </div> <!-- 黑星星 --> <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部分

<script>
    Vue.component("v-star",{
        template:"#v-star",props:{
            total:{http://www.cppcns.com
                default:10,},size:{
                default:30
            },// 接收從父元件傳過來的score
            value:{}
        },// 計算屬性
        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 + "%"
                }
            }http://www.cppcns.com
        },methods:{
            changeValue(value){
                // 將最新的結果傳給input
                // input標籤有有個預設的input事件
                this.$emit("input",value)
            }
        }
    })

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

效果圖

vue實現帶小數點的星星評分

vue實現帶小數點的星星評分

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。