期望实现上图效果,点击时切换样式。
<!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>