1 t( L* n) i' |( c2 H& }* N5 j
Vue.js classclass 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。 Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。 class 属性绑定我们可以为 v-bind:class 设置一个对象,从而动态的切换 class: 实例 1实例中将 isActive 设置为 true 显示了一个绿色的 div 块,如果设置为 false 则不显示: , i7 c3 N/ P. o" V# ^! P% R
" d! J6 h; l' H, Z& i9 A- p* p
- <div v-bind:class="{ active: isActive }"></div>
复制代码 以上实例 div class 为:
2 w: F2 w' X" U. c- <div class="active"></div>
复制代码我们也可以在对象中传入更多属性用来动态切换多个 class 。 实例 2text-danger 类背景颜色覆盖了 active 类的背景色:
$ T, n+ s- f, g) q
: R5 J9 n W# q4 j* S5 P- <div class="static"6 R: ~/ a% `; B4 a9 u, |, t/ k
- v-bind:class="{ active: isActive, 'text-danger': hasError }">
( Z4 D+ s( ^9 m& X7 B3 `/ C - </div>
复制代码 以上实例 div class 为:
5 e' _" ?# n4 p$ e- <div class="static active text-danger"></div>
复制代码我们也可以直接绑定数据里的一个对象: 实例 3text-danger 类背景颜色覆盖了 active 类的背景色: 9 m l* X) L4 Y3 e m: v
8 B" ]' p3 c* _, p n- <div id="app">+ f' ^- K4 U# g1 d
- <div v-bind:class="classObject"></div>
+ t8 I/ q. i, ]& ], U6 w - </div>
复制代码实例 3 与 实例 2 的渲染结果是一样的。 此外,我们也可以在这里绑定返回对象的计算属性。这是一个常用且强大的模式: 实例 4
( T+ P6 t0 B0 ?7 M% _6 z8 f* N
" n/ Y+ Q7 K n- q' e) q7 k- new Vue({4 A' e0 Y; a$ }7 m4 E! y
- el: '#app'," d' u: T0 N) X, e
- data: {
( O, T G' I( ]5 L& x/ U - isActive: true,8 D+ r% R" [& y2 N6 A K+ ]3 u5 {0 x
- error: null a3 ]2 u% a% g* L8 x- a( m: C
- },
0 `2 R) R: r' N+ Q( B9 g - computed: {) s4 ]2 I+ s1 u
- classObject: function () {
9 P' [4 w& _! J4 D$ F9 n - return {# a2 X" F3 w8 j) ]: h; v
- active: this.isActive && !this.error,
' R- V% N% l' D" g - 'text-danger': this.error && this.error.type === 'fatal',
( }3 ~! t y7 x - }
- a! m3 `. r0 Q- k* i: L& X& Z - }6 ?+ f0 N, j8 M% v+ t
- }' u9 u4 A% e- T+ F
- })
复制代码 数组语法我们可以把一个数组传给 v-bind:class ,实例如下: 实例 58 V5 C5 W- b+ `7 q9 f
, L m* t9 b. @- E. l8 }6 o- <div v-bind:class="[activeClass, errorClass]"></div>
复制代码 以上实例 div class 为:
9 Y9 E' O! _# E7 Y5 C2 O- <div class="active text-danger"></div>
复制代码我们还可以使用三元表达式来切换列表中的 class : 实例 6errorClass 是始终存在的,isActive 为 true 时添加 activeClass 类: * h* I1 \# N$ V! R6 i6 g
8 a/ R% [ E1 U
- <div v-bind:class="[errorClass ,isActive ? activeClass : '']"></div>
复制代码
g0 E) \* R! _ k4 J' d; x# T& \( c$ n" t
Vue.js style(内联样式)我们可以在 v-bind:style 直接设置样式: 实例 7
, v9 V& s+ a+ `: X% W
+ C8 b) F$ I- f- s- <div id="app">. j# S# u" D% k, {2 l+ a
- <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">菜鸟教程</div>
! ]" W( B. v: n0 p - </div>
复制代码 以上实例 div style 为:
7 k, I$ e$ H2 y: E- <div style="color: green; font-size: 30px;">菜鸟教程</div>
复制代码也可以直接绑定到一个样式对象,让模板更清晰: 实例 8
' `# _, T3 x0 Q( s% i% b/ {4 X# x* V" C5 d7 c
- <div id="app">( W% j4 a6 N0 |% Y6 j! n
- <div v-bind:style="styleObject">菜鸟教程</div>
% f/ B% f4 z) g! z3 ~% N' i s; h" c - </div>
复制代码v-bind:style 可以使用数组将多个样式对象应用到一个元素上: 实例 9
5 L' p8 b+ K5 R# }9 A4 A2 N& L9 E
# R6 Z( d6 i1 k8 f( f- <div id="app">6 y% P2 ? n9 @
- <div v-bind:style="[baseStyles, overridingStyles]">菜鸟教程</div>
; @ {2 e8 f3 o8 w0 ^- f - </div>
复制代码 1 V2 q. U7 K6 t) G( i
3 f/ {/ \) C$ K! {. O5 @
注意:当 v-bind:style 使用需要特定前缀的 CSS 属性时,如 transform ,Vue.js 会自动侦测并添加相应的前缀。. Z% y' b3 {4 T3 D% i. e
: h" J& A! `7 v0 h' D$ o
6 y* g$ S0 B5 x" ~3 d# w
' _5 v4 o+ s# Q/ s/ Y$ r: \6 g" {' U
|