yuminge-app/yun-min-program-plugin-master/mycomponent/star/star.js

65 lines
1.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Component({
properties: {
score: {
type: null
},
},
data: {
itemClassesData: []
}, // 私有数据,可用于模板渲染
lifetimes: {
// 生命周期函数可以为函数或一个在methods段中定义的方法名
attached() {
let itemClassesData = this.itemClasses();
this.setData({
itemClassesData:itemClassesData
});
},
moved() {},
detached() {},
},
// 生命周期函数可以为函数或一个在methods段中定义的方法名
attached() {}, // 此处attached的声明会被lifetimes字段中的声明覆盖
ready() {
},
pageLifetimes: {
// 组件所在页面的生命周期函数
show() {},
hide() {},
resize() {},
},
methods: {
itemClasses() {
let result = [];
let lengths = 5;
let score = Math.floor(this.data.score * 2) / 2; //例如把分数处理成在4.5以上及4.5就变成向上取整5在4.5以下就变成4.5
//是否需要半星
let starhalf = score % 1 != 0 ? true : false;
//几颗全星
let fullstar = Math.floor(score);
for (var i = 0; i < fullstar; i++) { //放全星
result.push('');
}
if (starhalf) { //放半星
result.push('half');
}
if (result.length < lengths) { //如果没有满到五个星就用灰色的星星补齐
var offstar = lengths - result.length;
for (var j = 0; j < offstar; j++) {
result.push('zo');
}
}
if (this.score <= 0.5) {
result = ['', '', '', '', ''];
}
return result;
}
}
});