请戳“极客小寨”关注
一起聊聊技术!
我们在学习微信小程序或者做项目时,应该会遇到五星评分效果情况,那么这个五星评分,半颗星星怎么做功能我们应该怎么编写呢?
今天我们说下微信小程序五星评分,半颗星星效果的实现,今天分享这样的小教程。希望对大家有所帮助。
不多说了,二当家要上图啦!


快去拿个小板凳,坐等跟多更新
<!--index.wxml-->
<block wx:for="{{stars}}" wx:key="">
<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>
//index.js
//获取应用实例
var app = getApp()
Page({
data: {
stars: [0, 1, 2, 3, 4],
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
})
}
})
/**index.wxss**/
.star-image{
position: absolute;
top: 50rpx;
width: 150rpx;
height: 150rpx;
src: "../../images/normal.png";
}
.item{
position: absolute;
top: 50rpx;
width: 75rpx;
height: 150rpx;
}
以上代码为效果为 图二

微信小程序独家秘笈之左滑删除
极客小寨:专注技术干货分享
围观
丨更多
热文
丨更多
热文
丨更多
热文
如何快速制作微信小程序预约功能
在这里只要十分钟
开发 | 小程序「分享图」生成难?
一招教你轻松解决
关于Geekxz的那些事儿
更多分享,请持续关注“极客小寨”

很多未公开的、隐藏的功能,用心去发现,惊喜总是会有的!
本文分享自微信公众号 - 编程微刊(wangxiaoting678)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
来源:oschina
链接:https://my.oschina.net/u/3704591/blog/4584453

