image 方式实现的笑脸评分功能
由于我这里的图片使用的不是背景图, 所以实现方法跟使用背景图的方式不同
你也可以使用 wx:for 来操作 由于微信小程序需要 base64 格式或者网图才能设置背景图 所以就没用背景图
首先这里放的是差中好评的三张图, 首先进入页面进行评分时需要默认是好评 1.PNG 表示的是未选中图片 2.PNG 代表的是选中的图片 图片点击绑定了同一个函数
- <view class='evaluation_top'>
- <view class='ico' bindtap='change_color' data-id='1'>
- <image class='ico_pace' src='../image / 差评{{ico_index1}}.png' style='width:80rpx;height:80rpx;'>
- </image>
差评
- </view>
- <view class='ico' bindtap='change_color' data-id='2'>
- <image class='ico_pace' src='../image / 中评{{ico_index2}}.png' style='width:80rpx;height:80rpx;'></image>
中评
- </view>
- <view class='ico' bindtap='change_color' data-id='3'>
- <image class='ico_pace' src='../image / 好评{{ico_index3}}.png' style='width:80rpx;height:80rpx;'></image>
好评
</view>
</view>
JS:
- Page({
- /**
- * 页面的初始数据 ico_index 默认表示的是三张图片是否被选中的状态 face_type 表示的是评分 差中好评分别对应分数为 1,3,5 因为当前默认好评所以评分默认为 5
- */
- data: {
- ico_index1:'1',
- ico_index2: '1',
- ico_index3: '2',
- face_type: '5',
- },
- change_color:function(e){
- var id = e.currentTarget.dataset.id; // 获取当前点击的是哪一个图片 通过 wxml 中 data-id 来判断的
- console.log(id);
- if (id==1){
- // 如果此时点击的是第 1 张图片 第 1 张图片变成 2.PNG, 则其他图片变成 1.PNG , 且此时评分变为 1 后面以此类推
- this.setData({
- ico_index1: '2',
- ico_index2: '1',
- ico_index3: '1',
- face_type: '1'
- })
- }
- if (id == 2) {
- this.setData({
- ico_index1: '1',
- ico_index2: '2',
- ico_index3: '1',
- face_type:'3'
- })
- }
- if (id == 3) {
- this.setData({
- ico_index1: '1',
- ico_index2: '1',
- ico_index3: '2',
- face_type:'5' //wxml 中直接通过 {
- {
- face_type
- }
- } 模板语言来使用
- })
- }
- },
- })
来源: https://www.jb51.net/article/150088.htm