@15013890200
2018-08-13T16:06:40.000000Z
字数 2627
阅读 904
vue
javascript
插件
<template>
<div>
<div class="j_div_score">
<div class="j_div_gray" :style="{cursor:disable ? '':'pointer'}">
<span v-for="i in arr" @click="setScore(i)" class="j_score_gray"></span>
<span class="j_sp_score" v-if="gold_width > 0">{{score}} 分</span>
</div>
<div class="j_div_gold" :style="{width:gold_width+'px'}">
<span :style="{cursor:disable ? '':'pointer'}" v-for="a in arr" @click="setScore(a)" class="j_score_gold"></span>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'myscore',
data(){
return{
total: 5,
arr: null,
gold_width: 0,
zero: false,
score: 2,
disable: false
}
},
props:{
scoreObj: {
type: Object,
default: null
}
},
mounted(){
if(this.arr === null)this.arr = [];
for(let i = 1; i <= this.total; i++){
this.arr.push(i);
}
if(this.score < 0)this.score = 0;
if(this.score > 10)this.score = 10;
this.gold_width = 22 * (this.score/2) + Math.floor(this.score/2)*5;
},
methods: {
setScore: function(i){
if(this.disable) return;
if(this.gold_width == 27 && i == 1 && this.zero){
this.gold_width = 0;
this.score = 0;
}
else{
this.gold_width = 27*i;
this.score = i * 2;
}
}
},
beforeMount(){
if(this.scoreObj){
for(let key in this.scoreObj){
this.$data[key] = this.scoreObj[key];
}
}
},
watch: {
'score': function(){
this.$emit('callback',this.score);
}
}
}
</script>
<style scoped>
.j_div_score{font-size: 14px;font-family: '微软雅黑';position:relative;user-select:none;}
.j_score_gray{background:url(imgs/gray.png) no-repeat;display: inline-block;width: 22px;height: 20px;margin-right:5px;}
.j_score_gold{background:url(imgs/gold.png) no-repeat;display: inline-block;width: 22px;height: 20px;margin-right:5px;}
.j_div_gray,.j_div_gold{float:left;position:absolute;top:0;left:0;height:20px;white-space:nowrap;}
.j_div_gold{overflow:hidden;transition:width 0.5s;}
.j_sp_score{display:inline-block;height:20px;line-height:20px;position:relative;top:-4px;color:#666;margin-left:10px;}
</style>
Tips:
import Vue from 'vue'
import vScore from './score.vue'
const score = {
name: 'myscore',
install: function(){
Vue.component('my-score',vScore);
}
};
export default score;
Tips:
my-score
可随意起,最好不要与HTML保留标签冲突
import vScore from '../components//vScore/score.js'
Vue.use(vScore)
<template>
<div>
<div class="div_score">
<my-score :scoreObj="scoreObj" @callback="getScore"></my-score>
</div>
</div>
</template>
<script>
import Vue from 'vue'
export default{
data(){
return{
scoreObj: {
score: 5.7,
disable: false,
zero: true
}
}
},
methods: {
getScore: function(s){
console.log(s);
}
}
}
</script>
属性名称 | 含义 | 默认值 | 是否必须 |
---|---|---|---|
score | 初始化评分插件 | 0 | 否 |
disable | 是否禁用 | true | 否 |
zero | 是否可以选择0分 | true | 否 |
Tips:
评分一般都是两分一跳,总分10分,考虑实际情况和美观,暂不开放对星星数量和每颗星对应的分值进行配置
@callback
处定义自己的回调方法