欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序 动态绑定事件并实现事件修改样式

程序员文章站 2022-11-14 16:43:30
微信小程序 动态绑定事件并实现事件修改样式 实例代码 wxml

微信小程序 动态绑定事件并实现事件修改样式

实例代码

wxml

<scroll-view class="reporttypescroll" scroll-x="true" scroll-left="{{toleftnum}}"> 
 <block wx:for-items="{{reporttypelist}}" wx:key="{{index}}"> 
  <text type="primary" style="width:{{itemwidth}}rpx;" bindtap="bind{{item.id}}" data-typeid="{{item.id}}" class="{{currentid==item.id?'clicksel':'removesel'}}">{{item.name}}</text> 
 </block> 
</scroll-view> 

js

var reporttypelist = [ 
 { name: "日报1", id: "1" }, 
 { name: "目录2", id: "2" }, 
 { name: "季报3", id: "3" }, 
 { name: "月报4", id: "4" }, 
 { name: "日报5", id: "5" }, 
 { name: "目录6", id: "6" }, 
 { name: "季报7", id: "7" }, 
 { name: "月报8", id: "8" }, 
 { name: "日报9", id: "9" }, 
 { name: "目录10", id: "10" }, 
 { name: "季报11", id: "11" }, 
 { name: "月报12", id: "12" }] 
 
var pageobject = { 
 data: { 
  reporttypelist: reporttypelist, 
  num: 0, 
  toleftnum: 0, 
  itemwidth: 150 
 } 
} 
 
for (var i = 0; i < reporttypelist.length; i++) { 
 (function (item) { 
  pageobject['bind' + item.id] = function (e) { 
   var id = parseint(e.currenttarget.dataset.typeid) 
 
   this.setdata({ 
    currentid: id 
   }) 
  } 
 })(reporttypelist[i]) 
} 
 
page(pageobject) 

wxss

/**index.wxss**/ 
 
.reporttypescroll { 
 margin: 40px 0px 20px 0px; 
 white-space: nowrap; 
 display: flex; 
} 
 
.clicksel { 
 color: red; 
} 
 
.removesel { 
 color: black; 
} 
 
text { 
 height: 150rpx; 
 display: inline-block; 
 text-align: center; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!