先看效果圖(忽略我家貓)

效果圖
原理是需要兩張圖片 一張選中的黃色,一張未選中的灰色

選中

未選中
1、循環(huán)image標(biāo)簽五次
2、綁定點(diǎn)擊事件
3、獲取點(diǎn)擊下標(biāo),并賦值給score變量
4、三目運(yùn)算判斷score變量于index 的關(guān)系 動(dòng)態(tài)更改圖片路徑
5、此方法網(wǎng)頁(yè)也可用 通用
-
//wxml
-
<image class="start"
-
wx:for="{{5}}"
-
data-index="{{index}}"
-
bindtap='selectIndexNum'
-
src="{{score >= index ? '/utils/img/big_star_s@3x.png' : '/utils/img/big_star_n@3x.png'}}"
-
/>
-
-
//普通版本 js
-
selectIndexNum(e){
-
this.setData({
-
score: e.currentTarget.dataset.index - 0
-
})
-
},
或者增強(qiáng)版效果
-
//點(diǎn)兩次相同分?jǐn)?shù)取消選擇 js
-
selectIndexNum(e){
-
let i = e.currentTarget.dataset.index;
-
if(i == this.data.score){
-
this.setData({
-
score: -1
-
})
-
}else {
-
this.setData({
-
score: e.currentTarget.dataset.index - 0
-
})
-
}
-
},
|