微信小程序 五星评分(包括半颗星评分)实例代码
2019/6/27 21:34:23
本文主要是介绍微信小程序 五星评分(包括半颗星评分)实例代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
微信小程序 五星评分
一位同学说要写五星评分.要有半颗星的评分.
于是我做了个玩具.有空了做模块化,这代码看不下去了.
代码:
1.index.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>
2.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; }
3.index.js
//index.js //CSDN微信小程序开发专栏:http://blog.csdn.net/column/details/13721.html //获取应用实例 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 }) } })
代码下载
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
这篇关于微信小程序 五星评分(包括半颗星评分)实例代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04package.json 文件位置在哪?-icode9专业技术文章分享
- 2024-10-01Craco.js学习:从入门到实践指南
- 2024-10-01Create-React-App学习:入门与实践指南
- 2024-10-01CSS-in-JS学习:从入门到实践指南
- 2024-09-30JSX语法学习:从入门到初步掌握
- 2024-09-30Mock.js学习:入门教程与实战演练
- 2024-09-30React Hooks学习:从入门到实践
- 2024-09-30受控组件学习:React中的基础入门教程
- 2024-09-29JS定时器教程:初学者必看指南
- 2024-09-29JS对象教程:初学者的全面指南