1.数组
:class="[‘name1’,‘name2’]"
2.三元表达式
:class="[‘name1’,flag?‘name2’:’’]"
3.嵌套
:class="[‘name1’,{‘name2’:flag}]"
:class={name2:flag} 只有一个属性进行调用时,[]和“”去掉后效果相同,可能会出现bug,出项后再进行添加
4.使用对象
:class="{name1:true,name2:false}"
:class=“mybody”
mybody:{name1:true,name2:false}
5.直接使用
class=“name1 name2 name3”
使用[]的代码中的调用属性需要加单引号;使用{}中的属性可直接使用,都需要在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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="./lib/vue.min.js"></script> <style> .big{ font-size: 50px; } .small{ font-size: 5px; } .blue{ color: darkblue; } .italic{ font-style: italic } </style> </head> <body> <div id="app"> <h5 class="big blue italic">H5大不大,瓦莱达</h5> <h5 :class=['big','blue']>H5大不大,瓦莱达</h5> <h5 :class=[flag?'big':'']>H5大不大,瓦莱达</h5> <h5 :class="[{blue:flag},'big']">H5大不大,瓦莱达</h5> <h5 :class="{big:true , blue:true}">H5大不大,瓦莱达555</h5> <h5 :class="mybody">H5大不大11111,瓦莱达</h5> <h5 :class={blue:flag}>瓦房大幅</h5> </div> <script> var vm = new Vue({ el:'#app', data:{ flag:true, mybody:{big:true , blue:true} }, methods:{} }) </script> </body> </html>
使用内联样式进行设置
1.使用style进行样式设置,进行绑定直接在对象上进行设置
:style="{‘color’:‘blue’,‘font-size’:‘75px’}"
color可以不加单引号,但是最好每个都加防止出现错误,不怕麻烦就怕bug
2.使用data进行设置
:style=“style1”
data:{
style1:{‘color’:‘blue’,‘font-size’:‘175px’}
}
3.从data中获得多个进行设置
:style="[style1,style2]"
data:{
style1:{‘color’:‘blue’,‘font-size’:‘175px’},
style2:{‘color’:‘blue’,‘font-size’:‘175px’}
}
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="./lib/vue.min.js"></script> <style> .thin{ font-size: 50px; font-weight: 100 } .blue{ color: darkblue; } .italic{ font-style: italic } </style> </head> <body> <div id="app"> <h1 class="blue italic">{{msg}}</h1> <h1 :class="['blue','thin']">{{msg}}</h1> <h1 :class="['blue',flag?'thin':'']">{{msg}}</h1> <h1 :class="['blue',{'thin':flag}]">{{msg}}</h1> <h1 :class="{blue:true,italic:true }">{{msg}}</h1> <h1 :class="mybody">{{msg}}</h1> <input type="text" name="" id="" v-model="msg"> <h1 :style="{'color':'blue','font-size':'75px'}">好不好</h1> <h1 :style="style1">好不好</h1> <h1 :style="[style1,style2]">好不好</h1> </div> <script> var vm = new Vue({ el:'#app', data:{ msg:'我好腻害', msg1:'blue', flag:false, mybody:{blue:true,italic:true,thin:true }, style1:{'color':'blue','font-size':'175px'}, style2:{'font-style':'italic'} }, methods:{ show(){ alert('dddd') } } }) </script> </body> </html>
最后
以上就是大气唇膏最近收集整理的关于vue3 class样式的全部内容,更多相关vue3内容请搜索靠谱客的其他文章。
发表评论 取消回复