微信小程序 动态绑定事件 且通过事件修改样式


//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;
}


  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值