cncml手绘网

标题: Vue.js 样式绑定 [打印本页]

作者: admin    时间: 2018-7-4 11:12
标题: Vue.js 样式绑定

* Y* X/ }) R6 P9 p. C) rVue.js class
class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。
Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。

class 属性绑定
我们可以为 v-bind:class 设置一个对象,从而动态的切换 class:
实例 1
实例中将 isActive 设置为 true 显示了一个绿色的 div 块,如果设置为 false 则不显示:

; r) d6 w; ^, ]7 `( `: g  p4 B& w7 D! B5 A& ^/ ?
  1. <div v-bind:class="{ active: isActive }"></div>
复制代码
以上实例 div class 为:
/ H( b. D: M' d" P  T/ R1 s
  1. <div class="active"></div>
复制代码
我们也可以在对象中传入更多属性用来动态切换多个 class 。
实例 2
text-danger 类背景颜色覆盖了 active 类的背景色:

& @# h$ M* h" U* `% t: D; v( U
1 S( I, K& L$ C2 k7 R/ u7 y6 {- ]
  1. <div class="static"4 \1 P, U0 K( W0 u( h- l
  2.      v-bind:class="{ active: isActive, 'text-danger': hasError }">
    & p1 K& D- o. @  I
  3. </div>
复制代码
以上实例 div class 为:
1 j" B3 s9 C& M" I9 M' P
  1. <div class="static active text-danger"></div>
复制代码
我们也可以直接绑定数据里的一个对象:
实例 3
text-danger 类背景颜色覆盖了 active 类的背景色:
) A, Z  @* V) I) O* w
' p6 p3 G  C) F- B" S
  1. <div id="app">& a$ ^$ l. l5 U
  2.   <div v-bind:class="classObject"></div>
    * x) l+ s! m1 n: N8 K
  3. </div>
复制代码
实例 3 与 实例 2 的渲染结果是一样的。
此外,我们也可以在这里绑定返回对象的计算属性。这是一个常用且强大的模式:
实例 4
/ x* f' Y; ~( c6 d5 \( N4 l4 s# d/ k; I6 u2 o6 q
  1. new Vue({
    * x, \" @" `0 O- B4 f
  2.   el: '#app',' j5 O5 R: ]6 X2 {  \, @
  3.   data: {
    0 l+ x4 e  R7 ]0 b5 D. E  H
  4.   isActive: true,/ n. m; ~0 t8 X1 e# w
  5.   error: null
    7 C5 T1 C* }% K; T+ J) a( q
  6.   },
      Y# J' d( \* m; o, a8 t
  7.   computed: {
    - W; B! V/ Z5 V- \2 i3 ?8 ?
  8.     classObject: function () {/ `7 }' l" ]0 h6 J; y8 Y
  9.       return {
    + ~, K( e3 |) ^! b7 o$ ^. i( z/ G9 ~, Z
  10.         active: this.isActive && !this.error,
    # L& e' E2 N% g; X( e! J+ C
  11.         'text-danger': this.error && this.error.type === 'fatal',
    # a; s, v% |8 ?" C5 {; s
  12.       }9 d$ P; f, i+ h
  13.     }
    9 `; t/ |; J$ S* f: E" {2 x7 i
  14.   }" h& X, I) |8 i, [
  15. })
复制代码
数组语法
我们可以把一个数组传给 v-bind:class ,实例如下:
实例 5$ w; Q0 o/ Q+ U: s. [
$ X, V3 T: v+ @8 G. d, q& E% J* }) \0 g! S
  1. <div v-bind:class="[activeClass, errorClass]"></div>
复制代码
以上实例 div class 为:" Y& B  o, B) J- S2 u) a- m/ @
  1. <div class="active text-danger"></div>
复制代码
我们还可以使用三元表达式来切换列表中的 class :
实例 6
errorClass 是始终存在的,isActive 为 true 时添加 activeClass 类:

' }; V$ g. F9 \1 [! o9 a2 j
5 D+ ?! C" y* Z3 ~# @. D
  1. <div v-bind:class="[errorClass ,isActive ? activeClass : '']"></div>
复制代码
: f& o. u4 i; M/ L! z

: l+ y% Z+ `  n! Z' FVue.js style(内联样式)
我们可以在 v-bind:style 直接设置样式:
实例 7. W, B' t- m7 V* y3 n) n" r& k
1 f8 }+ g( j0 l* t" Z
  1. <div id="app">
    4 Z, s6 ?1 i$ {7 h8 H
  2.     <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">菜鸟教程</div>+ w3 M: u; @9 H+ m7 s
  3. </div>
复制代码
以上实例 div style 为:
) `6 p: z+ J. L4 f1 X+ k. `6 [
  1. <div style="color: green; font-size: 30px;">菜鸟教程</div>
复制代码
也可以直接绑定到一个样式对象,让模板更清晰:
实例 8
7 @9 L2 t6 ?% E' o
! f! k4 k* _0 R
  1. <div id="app">, h, ~0 E0 i/ z  |4 s  ~# K7 i
  2.   <div v-bind:style="styleObject">菜鸟教程</div>9 k+ L4 ^9 U. k( _5 Q* i3 C
  3. </div>
复制代码
v-bind:style 可以使用数组将多个样式对象应用到一个元素上:
实例 9
) X7 d- H, n$ B5 [6 ~* r9 E0 w, L* I6 v6 Q' S. Z9 ]
  1. <div id="app">
    , M. ]! ?8 H2 o: ?6 y  u/ d3 S
  2.   <div v-bind:style="[baseStyles, overridingStyles]">菜鸟教程</div>
    - c, [9 d$ U& [; i) a. Y( C" s
  3. </div>
复制代码

+ Z2 {. s. C$ r* L  ~
; V" |' K$ _8 Z注意:当 v-bind:style 使用需要特定前缀的 CSS 属性时,如 transform ,Vue.js 会自动侦测并添加相应的前缀。. r) I; u7 o2 c- i8 z# M0 p" m

9 |" e; ?- V7 I4 A0 {$ a
; u2 i9 H5 G5 M; q' {, ?5 y& G4 |& F0 P/ n& a6 b
* q8 Q- @2 Q3 y9 j





欢迎光临 cncml手绘网 (http://www.cncml.com/) Powered by Discuz! X3.2