概述

class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。

Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。

class属性绑定

①使用属性值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue测试</title>
<script src="js/vue.js"></script>
<style type="text/css">
.active {
width: 100px;
height: 100px;
background: green;
}

.active2 {
width: 100px;
height: 100px;
background: red;
}
</style>
</head>
<body>
<div id="app">
第一种方式:
<div v-bind:class="isActive?'active':'active2'"></div>
第二种方式:
<div v-bind:class="{ 'active2': isActive,'active':!isActive }"></div>
<br>
<button v-on:click="modifyClass">改变样式</button>
</div>
<script type="text/javascript">
new Vue({
el: '#app',
data: {
isActive: true
},
methods: {
modifyClass: function () {
this.isActive = !this.isActive;
}
}
})
</script>
</body>
</html>

img

②使用对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue测试</title>
<script src="js/vue.js"></script>
<style type="text/css">
.active {
width: 100px;
height: 100px;
background: green;
}

.active2 {
width: 100px;
height: 100px;
background: red;
}
</style>
</head>
<body>
<div id="app">
<p>classObject: { 'active': {{classObject.active}}, 'active2': {{classObject.active2}} }</p>
<div v-bind:class="classObject"></div>
<br>
<button v-on:click="modifyClass">改变样式</button>
</div>
<script type="text/javascript">
new Vue({
el: '#app',
data: {
classObject: {
'active': true,
'active2': false
}
},
methods: {
modifyClass: function () {
this.classObject.active = !this.classObject.active;
this.classObject.active2 = !this.classObject.active2;
}
}
})
</script>
</body>
</html>

img

③数组语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue测试</title>
<script src="js/vue.js"></script>
<style type="text/css">
.active {
width: 100px;
height: 100px;
}

.active2 {
background: green;
}
</style>
</head>
<body>
<div id="app">
<p>class="{{activeClass}} {{active2Class}}"</p>
<div v-bind:class="[activeClass,active2Class]"></div>
</div>
<script type="text/javascript">
new Vue({
el: '#app',
data: {
activeClass: 'active',
active2Class: 'active2'
}
})
</script>
</body>
</html>

img

Vue.js style(内联样式)

①在 v-bind:style 直接设置样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<div id="app">
<p v-bind:style="{color:fontColor,fontSize:fontSize+'px'}">LeDao的博客</p>
<button v-on:click="bigFontSize">增大字号</button>
<button v-on:click="smallFontSize">减小字号</button>
</div>
<script type="text/javascript">
new Vue({
el: '#app',
data: {
fontColor: 'red',
fontSize: 15
},
methods: {
bigFontSize: function () {
this.fontSize++;
},
smallFontSize: function () {
this.fontSize--;
}
}
})
</script>

img

②绑定一个对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<div id="app">
<p v-bind:style="styleObject">LeDao的博客:http://www.zoutl.cn</p>
</div>
<script type="text/javascript">
new Vue({
el: '#app',
data: {
styleObject: {
color: 'red',
fontSize: '33px'
}
}
})
</script>

③绑定多个对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<div id="app">
<p v-bind:style="[style1,style2]">LeDao的博客</p>
</div>
<script type="text/javascript">
new Vue({
el:'#app',
data:{
style1:{
'color':'green',
'font-size':'33px'
},
style2:{
'font-weight':'bold'
}
}
})
</script>