vue中按钮使⽤v-bind:class动态切换颜⾊(点击A号button,
背景⾊变化;点。。。
当前Vue项⽬需要做⼀个按钮切换的功能(点击A号button,背景⾊变化;点击B号button,A号回复原背景⾊,B变⾊)
=====》 =====》
实现上述功能,最基础的⽅法就是,动过获取对应的dom,强制改变背景⾊:
<div class="button1" v-on:click="YGDWKGMJ()" ref="ygdwkgmj">开发投资</div>
<div class="button2" v-on:click="XKGMJ()" ref="xkgmj">成交均价</div>
<div class="button2" v-on:click="LMDJ()" ref="lmdj">销售⾯积</div>
毛晓彤个人资料<div class="button2" v-on:click="CLZFCJMJ()" ref="clzfcjmj">库存监测</div>
YGDWKGMJ() {
this.$dwkgmj.style.backgroundColor = "#3657be";
this.$refs.xkgmj.style.backgroundColor = "#192e5b";
this.$refs.lmdj.style.backgroundColor = "#192e5b";
this.$refs.clzfcjmj.style.backgroundColor = "#192e5b";
},
这种⽅式,虽然能实现功能,但是代码臃肿,后期维护不便。
===============================================================
还有⼀种⽅法就是通过传给v-bind:class⼀个对象,以动态地切换 class
<li v-for="(list,index) in leftPart" class="aa" @click="leftChange(index)" :class="{ liBackground:changeLeftBackground == index}">{{list.name}}</li>
data() {
return {
leftPart: [
{ name: "开发投资" },
泫雅分手邓紫棋方回应打钱{ name: "成交均价" },喜剧片电影
{ name: "销售⾯积" },
{ name: "库存监测" }
],
changeLeftBackground: 0,
};
},
methods: {
leftChange(index) {
this.changeLeftBackground = index;
特工类小说
},
}
<style scoped>
.liBackground {
赵本山绯闻background: -webkit-gradient(linear, 0 0, 0 100%, from(#303fb2), to(#2f70d4));
}
</style>
发布评论