vue實現(xiàn)帶小數(shù)點的星星評分
本文實例為大家分享了vue實現(xiàn)帶小數(shù)點的星星評分的具體代碼,供大家參考,具體內(nèi)容如下
首先我們要先引入vue.js文件
css部分
<style> main{ position:relative; } .star_line{ /* 設(shè)置強制不換行 */ width-space: nowrap; overflow: hidden; position: absolute; } .star{ display: inline-block; /* 設(shè)置當(dāng)鼠標(biāo)放到星星上是變成小手樣式 */ cursor: pointer } </style>
body部分
<div id="app"> <input type="text" v-model.number="score"> <- 任何一個組件在進(jìn)行雙向綁定接收綁定的值的時候,必須使用value來接收,原理參考input -> <v-star v-model="score"></v-star> </div>
js部分我們用到組件,input在根組件內(nèi),而我們創(chuàng)建的星星放在一個組件內(nèi),主要通過雙向綁定,父組件和子組件相互傳值,來實現(xiàn)星星評分
組件模板部分
<script id="v-star" type="text/html"> <main :style="mainStyle"> <!-- 白星星 --> <div class="star_line"> <span @click="changeValue(star)" class="star" :style="starStyle" v-for="star in total">☆</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:{ 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 + "%" } } }, methods:{ changeValue(value){ // 將最新的結(jié)果傳給input // input標(biāo)簽有有個默認(rèn)的input事件 this.$emit("input",value) } } }) new Vue({ el:"#app", data:{ score:1 } }) </script>
效果圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Element樹形控件el-tree實現(xiàn)一鍵全選、反選功能
最近做的項目用到了全選全不選功能,于是就自己動手寫了一個,這篇文章主要給大家介紹了關(guān)于Element樹形控件el-tree實現(xiàn)一鍵全選、反選功能的相關(guān)資料,需要的朋友可以參考下2023-10-10關(guān)于Vue?CLI3中啟動cli服務(wù)參數(shù)說明
這篇文章主要介紹了關(guān)于Vue?CLI3中啟動cli服務(wù)參數(shù)說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04Vue+ElementUI實現(xiàn)表單動態(tài)渲染、可視化配置的方法
這篇文章主要介紹了Vue+ElementUI實現(xiàn)表單動態(tài)渲染、可視化配置的方法,需要的朋友可以參考下2018-03-03Vue 2.0學(xué)習(xí)筆記之Vue中的computed屬性
本篇文章主要介紹了Vue 2.0學(xué)習(xí)筆記之Vue中的computed屬性,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10