2021-07-06 使用v-bind设置样式属性:class与style设置元素样式

本文详细介绍了Vue中如何使用v-bind:class和v-bind:style来动态设置元素样式。v-bind:class可以接受对象、数组、计算属性等不同形式的数据,实现动态切换class。而v-bind:style则可以直接设置样式,也可以绑定到样式对象或数组,甚至处理需要前缀的CSS属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

v-bind:class设置样式

1.为v-bind:class设置一个对象,实现动态切换class
示例

html:

<div id="app">
	<div v-bind:class="{'box':xianshi}">hello</div>
</div>

css:

.box {color: yellow;}

js:

new Vue({
    el: "#app",
    data: {xianshi: true}//false就不显示
});
2.在对象中传入更多属性,动态切换更多的class
示例

html:

<div id="app">
	<div v-bind:class="{'box1':yangshi1,'box2':yangshi2}">hello</div>
</div>

css:

.box1 {color: red;}
.box2 {color: blue;}

js:

new Vue({
    el: "#app",
    data: {
        yangshi1: true,
        yangshi2: false
        /*哪个true就输入哪个,且后面的样式权重高,都是false则不显示样式*/
    }
});
3.直接绑定数据里的一个对象
示例:

html:

<div id="app">
	<div v-bind:class="obj">hello</div>
</div>

css:

.box {color: red;}

js:

new Vue({
    el: "#app",
    data: {obj:{"box":true}}
});
4.绑定返回对象的计算属性
示例:

html:

<div id="app"><div v-bind:class="obj">hello</div></div>

css:

.box1 {background: green;}
.box2 {background: red;}

js:

new Vue({
    el: '#app',
    data: {
        show1: true,
        judge: {
            show2: false,
            error: 'cuowu'
        }
    },
    computed: {//计算属性
        obj: function() {
            return {
                box1: this.show1 && !this.judge.show2,
                box2: this.judge.show2 && this.judge.error === 'cuowu',
            }
        }
    }
})
5.把一个数组传给v-bind:class
示例

html:

<div id="app">
	<div v-bind:class="[sub1,sub2]">hello</div>
</div>

css:

.box1 {background: green;} 
.box2 {background: red;}

js:

new Vue({
    el: "#app",
    data: {
        sub1: 'box1',
        sub2: 'box2'
    }
});
输出:

输出:

hello

6.使用三元表达式来切换列表中的class
示例

html:

<div id="app">
	<div v-bind:class="[sub1,judge?sub2:'']">hello</div>
</div>

css:(同上)
js:

new Vue({
    el: "#app",
    data: {
        judge: true,
        sub1: "box1",
        sub2: "box2"
    }
});

v-bind:style设置样式

1.style直接设置样式
示例

html:

<div id="app">
	<div v-bind:style="{color:c,fontSize:fs+'px'}">hello</div>
</div>

js:

new Vue({
    el: "#app",
    data: {
        c: 'red',
        fs: 18
    }
});
2.直接绑定到一个样式对象上
示例

html:

<div id="app">
	<div v-bind:style="Obj">hello</div>
</div>

js:

new Vue({
    el: "#app",
    data: {
        Obj: {
            color: 'red',
            fontSize: '28px'
        }
    }
});
3.使用数组将多个样式对象应用到一个元素上
示例

html:

<div id="app">
	<div v-bind:style="[sub1,sub2]">hello</div>
</div>

js:

new Vue({
    el: "#app",
    data: {
        sub1: {color: "green"},
        sub2: {
            fontSize: "20px",
            "font-weight": "bolder"
        }
    }
});

*当v-bind:style使用需要特定前缀放的css属性(如:transform)时,vue会自动检测并添加相应前缀

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端OnTheRun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值