被点击触发事件效果

被点击触发事件效果

wbe前端 小程序

功能:点击左侧菜单中的内容,显示红色字体和左边框,并在右侧有相应商品内容显示

效果如图:
在这里插入图片描述

1 获取被点击标题身上的索引

2 给 data 中的 currentIndex 赋值

3 根据不同的索引来渲染右侧的商品内容

在wxss先给描述被点击事件的动作效果:

.active{
      color:var(--themeColor);
      border-left: 5rpx solid currentColor;
    }

在js中的data里面加入 currentIndex 存放当前索引数据:

data: {
      //左侧的菜单数据
      leftMenuList:[],
      //右侧商品数据
      rightContentList:[],
      //被点击的左侧的菜单索引
      currentIndex:0,
  },

在wxml的循环里加入 条件样式{{index===currentIndex? ‘active’:’’}} ,然后绑定事件handleItemTap ,让 data-index 传递数据

<!-- 左侧菜单 开始 -->
    <scroll-view class="left_menu" scroll-y="true" >
      <view class="menu_item {{index===currentIndex? 'active':''}}"
      wx:for="{{leftMenuList}}"
      wx:key="*this"
      wx:for-item="item"
      wx:for-index="index"
      bindtap="handleItemTap"
      data-index="{{index}}"
      >
        {{item}}
      </view>
    </scroll-view>

回到 js 加入 左侧菜单的点击事件 handleItemTap ,先利用console.log(e)找出被点击菜单的索引信息在 e.currentTarget.dataset
在这里插入图片描述

handleItemTap(e){
    const{index}=e.currentTarget.dataset;
    //根据左侧不同的索引值来渲染右侧的商品内容
    let rightContentList = this.Cates[index].children;
    this.setData({
      currentIndex:index,
      rightContentList,
    })
    //console.log(e);
  }

完成!查看效果
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值