小程序实现多个按钮点击跳转动态更换样式

 .swan页面代码:判断index与currentIndex的值是否相同,相同的话就绑定.active,不同的话就不绑定该样式

<view class="fourBtns">
        <view s-for="item,index in fourBTN" bindtap="itemClick" data-idx='{{index}}'
            class="fourBtn {{index==currentIndex?'active':''}}">
            {{fourBTN[index]}}
        </view>
        <image src="tehui.png" class="tehui"></image>
        <image src="hot.png" class="hot"></image>
    </view>

css页面代码: 

.active{
    background-image: url("../images/fourBG.png");
    background-repeat: no-repeat;
    background-size: 11vh;
    background-position:center;
    color: white;
}

.js页面代码:


Page({
       data: {
           fourBTN: ['A', 'B', 'C', 'D'],
            currentIndex: 1,
         },
        //四个按钮点击,每次点击都要设置记录的currentIndex等于当前的index
            itemClick(e) {    
                console.log("按钮 " + e.currentTarget.dataset.idx + " 被点击了!");
                this.setData({
                    currentIndex: e.currentTarget.dataset.idx
                });
                if (e.currentTarget.dataset.idx == 0) {
                    swan.navigateBack({
                        url: `../list/list`
                    });
                }
            },
        })

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值