柿霖不是林 发表于 2017-2-15 15:06:44

微信小程序demo:五星评分(含半颗星评分)

一位同学说要写五星评分.要有半颗星的评分.
于是我做了个玩具.有空了做模块化,这代码看不下去了.



WXML
<!--index.wxml-->
<block wx:for="{{stars}}">
<image class="star-image" style="left: {{item*150}}rpx" src="{{key > item ?(key-item == 0.5?halfSrc:selectedSrc) : normalSrc}}">
    <view class="item" style="left:0rpx" data-key="{{item+0.5}}" bindtap="selectLeft"></view>
    <view class="item" style="left:75rpx" data-key="{{item+1}}" bindtap="selectRight"></view>
</image>
</block>




WXSS
.star-image {
position: absolute;
top: 50rpx;
width: 150rpx;
height: 150rpx;
src: "../../images/normal.png";
}

.item {
position: absolute;
top: 50rpx;
width: 75rpx;
height: 150rpx;
}


JS


//index.js
//CSDN微信小程序开发专栏:http://blog.csdn.net/column/details/13721.html
//获取应用实例
var app = getApp()
Page({
data: {
    stars: ,
    normalSrc: '../../images/normal.png',
    selectedSrc: '../../images/selected.png',
    halfSrc: '../../images/half.png',
    key: 0,//评分
},
onLoad: function () {
},
//点击右边,半颗星
selectLeft: function (e) {
    var key = e.currentTarget.dataset.key
    if (this.data.key == 0.5 && e.currentTarget.dataset.key == 0.5) {
      //只有一颗星的时候,再次点击,变为0颗
      key = 0;
    }
    console.log("得" + key + "分")
    this.setData({
      key: key
    })

},
//点击左边,整颗星
selectRight: function (e) {
    var key = e.currentTarget.dataset.key
    console.log("得" + key + "分")
    this.setData({
      key: key
    })
}
})




**** Hidden Message *****

Ywenli 发表于 2017-8-18 11:28:20

感谢分享

jack56846 发表于 2020-4-20 11:27:51

专注于与个人游戏开发者合作,有产品的朋友联系微,信,号j i m 2 0 1 8 0 6 8 8,详聊
页: [1]
查看完整版本: 微信小程序demo:五星评分(含半颗星评分)