使用vue实现样式切换

9 篇文章 0 订阅

请添加图片描述
期望实现上图效果,点击时切换样式。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script crossorigin="anonymous" integrity="sha512-XdUZ5nrNkVySQBnnM5vzDqHai823Spoq1W3pJoQwomQja+o4Nw0Ew1ppxo5bhF2vMug6sfibhKWcNJsG8Vj9tg==" src="https://lib.baomitu.com/vue/2.6.14/vue.min.js"></script>
  <title>change style</title>
  <style>
    .toggle-btn {
      font-family: 'Arial Normal', 'Arial', sans-serif;
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-bottom: 1px solid #dcdcdc
    }

    .toggle-btn .order-btn-default {
      width: 120px;
      margin: 0;
      border: 1px solid rgba(170, 170, 170, 1);
      border-right: none;
      font-size: 14px;
      color: #AAAAAA;
      background-color: rgba(255, 255, 255, 1);
      box-sizing: border-box;
      padding: 2px;
      height: 29px;
    }
    .toggle-btn-container button:last-child {
      border-right: 1px solid rgba(170, 170, 170, 1);
    }
    .toggle-btn .order-btn-active {
      background-color: rgba(122, 168, 246, 1);
      color: #FFFFFF;
    }
  </style>
</head>

<body>
  <div id="v-page">
    <div class="toggle-btn">
      <div class="toggle-btn-container">
        <button type="button" @click="addClass(index)" v-for="(item, index) in arr" :key="index" v-text="item" :class="['order-btn-default', selected==index? 'order-btn-active' : '']"></button>
      </div>
    </div>
  </div>

</body>
<script>
  var vm = new Vue({
    el: '#v-page',
    data() {
      return {
        selected: 0,
        arr: ['by second', 'by minute', 'by hour', 'by day', 'by month', 'by year']
      }
    },
    methods: {
      addClass(index) {
        this.selected = index;
      }
    }
  })
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值