" ]/ O4 l5 A, x9 [Vue.js classclass 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。 Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。 class 属性绑定我们可以为 v-bind:class 设置一个对象,从而动态的切换 class: 实例 1实例中将 isActive 设置为 true 显示了一个绿色的 div 块,如果设置为 false 则不显示: & w! W# h; G- d6 q& x+ `' v
, N- w& t# M+ P) f
- <div v-bind:class="{ active: isActive }"></div>
复制代码 以上实例 div class 为:3 r2 L4 ^7 Q" a; x. C) i. R
- <div class="active"></div>
复制代码我们也可以在对象中传入更多属性用来动态切换多个 class 。 实例 2text-danger 类背景颜色覆盖了 active 类的背景色: 0 P" i1 y9 L5 R @; D4 Q+ p
5 z) F% f3 H2 s) I, [- \3 ~- <div class="static"5 K% Q0 t& `; W/ f0 y$ r ~- R3 o. p
- v-bind:class="{ active: isActive, 'text-danger': hasError }">; P/ b! u2 V2 B1 x: W0 x- b% P
- </div>
复制代码 以上实例 div class 为:
/ I( b4 m& S/ z3 g' }6 G2 n- <div class="static active text-danger"></div>
复制代码我们也可以直接绑定数据里的一个对象: 实例 3text-danger 类背景颜色覆盖了 active 类的背景色:
+ [# y3 F9 _9 w J" n$ `
' b2 ~' T# H. M, R- <div id="app">
4 \- |( Q, o6 R% n: k% G - <div v-bind:class="classObject"></div># }) L6 b! _, H2 s# m
- </div>
复制代码实例 3 与 实例 2 的渲染结果是一样的。 此外,我们也可以在这里绑定返回对象的计算属性。这是一个常用且强大的模式: 实例 4
% ] F' C0 S P$ x0 t8 V/ Q u1 T: ?( W
- new Vue({
6 u2 M, Y' N; {2 u - el: '#app',* l- J0 u3 K, f/ e5 ?% m
- data: {
! k' J% L9 P) `4 T - isActive: true,
/ f q; i' D: N7 ^ - error: null
i& k! q7 ]/ M7 J' X$ f9 i - },
7 ]0 K$ c5 F% V5 h1 z8 O n3 K7 Z - computed: {
! G' {0 P0 \8 H1 z) P7 P @( m# e - classObject: function () {$ E2 u6 \5 P8 h0 E* H e
- return {
4 ~2 s8 c+ E' ^& g( K0 E. [' { - active: this.isActive && !this.error,, ^4 M. u! K% I, H* S
- 'text-danger': this.error && this.error.type === 'fatal',
, D( h! o# F: \( Q. J" y+ X. y - }
) ~/ i( n# [3 [, }( l% o - }
1 P: @- D( ?1 q! p5 Y - }2 N- [# Y3 b" C' b& Q+ n0 z) Y7 J
- })
复制代码 数组语法我们可以把一个数组传给 v-bind:class ,实例如下: 实例 5
" h8 \2 X$ T' f% X C9 j2 H5 S" }+ e! P9 z: p1 L4 x/ e z& R
- <div v-bind:class="[activeClass, errorClass]"></div>
复制代码 以上实例 div class 为:: _& M2 `8 x9 {# e: Y7 \
- <div class="active text-danger"></div>
复制代码我们还可以使用三元表达式来切换列表中的 class : 实例 6errorClass 是始终存在的,isActive 为 true 时添加 activeClass 类: : K I3 _* k3 W A
& ]# ^$ _! j! }/ R9 i+ H- <div v-bind:class="[errorClass ,isActive ? activeClass : '']"></div>
复制代码
9 }5 w! R& Z, j3 V9 @, P
% z4 ^ q% Z+ p m! `: t ^Vue.js style(内联样式)我们可以在 v-bind:style 直接设置样式: 实例 7
7 W( @5 f! Q7 y" N) R# w! P8 S+ _4 B" e0 I) G; }, }
- <div id="app">
5 y9 t+ F, c& {% E! X+ ^ - <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">菜鸟教程</div>
# P, p) ]5 |8 N+ D2 Y5 F b - </div>
复制代码 以上实例 div style 为:
% V' J8 L0 a# _- <div style="color: green; font-size: 30px;">菜鸟教程</div>
复制代码也可以直接绑定到一个样式对象,让模板更清晰: 实例 8
+ ~ D' d& _8 X f! D/ v% G! K$ b) i/ i6 w
- <div id="app">
, }9 Q5 }! M6 j3 W4 M+ l0 ~ - <div v-bind:style="styleObject">菜鸟教程</div>
/ w9 ~ u& m7 Z) F1 y# L3 H - </div>
复制代码v-bind:style 可以使用数组将多个样式对象应用到一个元素上: 实例 91 D* w: |5 C' @; a
/ q4 U$ t9 f, Y2 X' E9 o
- <div id="app">( R7 Y. |+ n/ Z: h
- <div v-bind:style="[baseStyles, overridingStyles]">菜鸟教程</div>% Q1 n& V% S. ` n" L# }7 M
- </div>
复制代码
) z1 B8 c% f$ _8 q
. S7 N/ M8 B9 x/ L1 V1 V注意:当 v-bind:style 使用需要特定前缀的 CSS 属性时,如 transform ,Vue.js 会自动侦测并添加相应的前缀。" C6 @' [. n- L* @1 S' |2 e0 {8 r
9 R) y2 b5 s# b5 I; v0 r
; U( c1 q$ P, n! k- {% ]
O# r3 d' A7 H
& ^3 z: A# V$ ?, m6 L# J; q! j |