用vue简单的实现点击按钮切换背景颜色

用vue简单的实现点击按钮切换背景颜色

  <div class="btnTitle">
      <div class="btn-bg" :class="{bg:time == 3}" @click="changeBg(3)">15</div>
      <div class="btn-bg" :class="{bg:time == 4}" @click="changeBg(4)">30</div>
  </div>
  <div class="btnTitle">
       <div class="btn-bg" :class="{bg:increase == 5}" @click="changeBg(5)">增量</div>
       <div class="btn-bg" :class="{bg:increase == 6}" @click="changeBg(6)">总量</div>
   </div>
   // 在methods方法里面
   data () {
   			time: 3,
      		increase: 5, 
   },
    methods: {
    changeBg (index) {
      if (index === 3 || index === 4) {
        this.time = index
      } else if (index === 5 || index === 6) {
        this.increase = index
      } else {
        this.sort = index
      }
    }
  }
  // css样式
  <style lang="scss">
		.bg {
		      background-color: rgb(12, 197, 89);
		       color: #fff;
	     }
	     .btnTitle {
             display: flex;
             width: 400px;
             border: 1px solid #ccc;
             background-color: #fff;
             margin-left: 10px;
             border-radius: 5px;
             .btn-bg {
                 width: 50%;
                 height: 40px;
                 text-align: center;
                 line-height: 40px;
                 border-radius: 5px;
             }
         }
  </style>

点击前
在这里插入图片描述
点击后
在这里插入图片描述
就这样简单的切换效果就达到了!!!!

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值