5.Class與style綁定

數(shù)據(jù)綁定一個常見需求是操作元素的 class 列表和它的內(nèi)聯(lián)樣式。因為它們都是屬性 ,我們可以用v-bind 處理它們:只需要計算出表達式最終的字符串。不過,字符串拼接麻煩又易錯。因此,在 v-bind 用于 class 和 style 時, Vue.js 專門增強了它。表達式的結(jié)果類型除了字符串之外,還可以是對象或數(shù)組。

綁定 HTML Class

對象語法

我們可以傳給 v-bind:class 一個對象,以動態(tài)地切換 class 。

<div v-bind:class="{ active: isActive }"></div>

上面的語法表示 classactive 的更新將取決于數(shù)據(jù)屬性 isActive 是否為真值 。

我們也可以在對象中傳入更多屬性用來動態(tài)切換多個 class 。此外, v-bind:class 指令可以與普通的 class 屬性共存。如下模板:

<div class="static"
     v-bind:class="{ active: isActive, 'text-danger': hasError }">
</div>

如下 data:

data: {
  isActive: true,
  hasError: false
}

渲染為:

<div class="static active"></div>

當 isActive 或者 hasError 變化時,class 列表將相應(yīng)地更新。例如,如果 hasError 的值為 true , class列表將變?yōu)?"static active text-danger"。

你也可以直接綁定數(shù)據(jù)里的一個對象:

<div v-bind:class="classObject"></div>
data: {
  classObject: {
    active: true,
    'text-danger': false
  }
}

渲染的結(jié)果和上面一樣。我們也可以在這里綁定返回對象的計算屬性。這是一個常用且強大的模式:

<div v-bind:class="classObject"></div>
data: {
  isActive: true,
  error: null
},
computed: {
  classObject: function () {
    return {
      active: this.isActive && !this.error,
      'text-danger': this.error && this.error.type === 'fatal',
    }
  }
}

數(shù)組語法

我們可以把一個數(shù)組傳給 v-bind:class ,以應(yīng)用一個 class 列表:

<div v-bind:class="[activeClass, errorClass]">
data: {
  activeClass: 'active',
  errorClass: 'text-danger'
}

渲染為:

<div class="active text-danger"></div>

如果你也想根據(jù)條件切換列表中的 class ,可以用三元表達式:

<div v-bind:class="[isActive ? activeClass : '', errorClass]">

此例始終添加 errorClass ,但是只有在 isActive 是 true 時添加 activeClass 。

不過,當有多個條件 class 時這樣寫有些繁瑣。可以在數(shù)組語法中使用對象語法:

<div v-bind:class="[{ active: isActive }, errorClass]">
With Components

This section assumes knowledge of Vue Components. Feel free to skip it and come back later.

When you use the class attribute on a custom component, those classes will be added to the component’s root element. Existing classes on this element will not be overwritten.

For example, if you declare this component:

Vue.component('my-component', {
  template: '<p class="foo bar">Hi</p>'
})
Then add some classes when using it:

<my-component class="baz boo"></my-component>
The rendered HTML will be:

<p class="foo bar baz boo">Hi</p>
The same is true for class bindings:

<my-component v-bind:class="{ active: isActive }"></my-component>
When isActive is truthy, the rendered HTML will be:

<div class="foo bar active"></div>

綁定內(nèi)聯(lián)樣式

對象語法

v-bind:style 的對象語法十分直觀——看著非常像 CSS ,其實它是一個 JavaScript 對象。 CSS 屬性名可以用駝峰式(camelCase)或短橫分隔命名(kebab-case):

<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
data: {
  activeColor: 'red',
  fontSize: 30
}

直接綁定到一個樣式對象通常更好,讓模板更清晰:

<div v-bind:style="styleObject"></div>
data: {
  styleObject: {
    color: 'red',
    fontSize: '13px'
  }
}

同樣的,對象語法常常結(jié)合返回對象的計算屬性使用。

數(shù)組語法

v-bind:style 的數(shù)組語法可以將多個樣式對象應(yīng)用到一個元素上:

<div v-bind:style="[baseStyles, overridingStyles]">

自動添加前綴

當 v-bind:style 使用需要特定前綴的 CSS 屬性時,如 transform ,Vue.js 會自動偵測并添加相應(yīng)的前綴。
根據(jù)上面的內(nèi)容寫出的例子如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>level04</title>
<script type="text/javascript" src="js/vue.2.1.0.js"></script>
</head>
<body>
<div id="app">
    <div v-bind:class="{testcss:flag1}">testcss</div>
    <div class='selfcss' v-bind:class="{truecss: flag1, 'false-css': flag1}">testcss</div>
    <div class='selfcss' v-bind:class="classObject">objectcss</div>
    <div class='selfcss' v-bind:class="[flag1?'truecss':'falsecss']">arraycss</div>
</div>
<script type="text/javascript">


var app = new Vue({
    el:'#app'
    ,data:{
        flag1:true
        ,classObject: {
            active: true,
            'text-danger': false
        }
    }
});


</script>
</body>
</html>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,661評論 19 139
  • Class 與 Style 綁定 數(shù)據(jù)綁定的一個常見需求是操作元素的 class 列表和它的內(nèi)聯(lián)樣式。因為它們都是...
    李諾哦閱讀 656評論 0 0
  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內(nèi)容,還有我對于 Vue 1.0 印象不深的內(nèi)容。關(guān)于...
    云之外閱讀 5,180評論 0 29
  • 本文為轉(zhuǎn)載,原文:Vue學(xué)習(xí)筆記入門篇——Class 與 Style 綁定 綁定HTML Class 數(shù)據(jù)綁定一個...
    ChainZhang閱讀 709評論 0 2
  • 如果可以,時間可以有無數(shù)個讀法。 在這里我說說我理解的三種讀法。 第一種讀法 房間的間 時間好像是一間房間,可以...
    痞馬閱讀 336評論 0 0

友情鏈接更多精彩內(nèi)容