<body>
<div id="root">
方法一:
<p :class="{active:isactive}" @click="handleChangeColorOne">{{message}}</p>
方法二:
<p :class="['color-gray',actived]" @click="handleChangeColorTwo">{{message}}</p>
方法三:
<p :style="colorObj1" @click="handleChangeColorThree">{{message}}</p>
方法四:
<p :style="[colorObj2,{fontSize:'28px'}]" @click="handleChangeColorFour">{{message}}</p>
</div>
</body>
<script>
var vm = new Vue({
el: '#root',
data: {
message: 'hello wrold',
isactive: false,
actived: '',
colorObj1: {
color: 'pink'
},
colorObj2: {
color: 'green'
}
},
methods: {
// 点击切换颜色
handleChangeColorOne() {
this.isactive = this.isactive ? false : true
},
handleChangeColorTwo() {
this.actived = this.actived == 'active' ? '' : 'active'
},
handleChangeColorThree() {
this.colorObj1.color = this.colorObj1.color == 'pink' ? '' : 'pink'
},
handleChangeColorFour() {
this.colorObj2.color = this.colorObj2.color == 'green' ? '' : 'green'
}
}
})
</script>
<style>
p {
/* 默认颜色 */
color: rgb(0, 255, 94);
}
.color-gray {
color: gray;
}
.active {
color: #f00;
}
</style>
vue中动态绑定样式
最新推荐文章于 2025-06-11 20:06:40 发布