微信小程序實現給循環列表添加點擊樣式實例


 

微信小程序有個屬性hover-class='active',是指當點擊列表元素時當按下鼠標左鍵會顯示active樣式,但是鼠標離開樣式就會復原.可以參考以下解決方案,直接上代碼: 

wxml:

?
1
2
3
4
5
6
7
8
<view class="taga">
 <view class="tag-title">標簽</view>
 <view class="tag-box">
 <view wx:for="{{taga}}" wx:key="id" wx:for-index="i">
 <view class="taga-item {{currentItem==item.id?'active-tag':''}}" data-id="{{item.id}}" bindtap="tagChoose">{{item.name}}</view>
 </view>
 </view>
 </view>

js文件:

?
1
2
3
4
5
6
7
8
9
10
11
tagChoose:function(options){
var that = this
var id = options.currentTarget.dataset.id;
console.log(id)
//設置當前樣式
that.setData({
'currentItem':id
})
 
 
}

核心點:class=”taga-item {{dateCurrent==item.id?'active-tag':”}}”模板文件中使用三元運算符,通過dateCurrent指定當前item的id

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM