Vue.2.0.5實(shí)現(xiàn)Class 與 Style 綁定的實(shí)例
Class 與 Style 綁定
數(shù)據(jù)綁定一個(gè)常見需求是操作元素的 class 列表和它的內(nèi)聯(lián)樣式。因?yàn)樗鼈兌际菍傩?,我們可以用v-bind 處理它們:只需要計(jì)算出表達(dá)式最終的字符串。不過,字符串拼接麻煩又易錯(cuò)。因此,在 v-bind 用于 class 和 style 時(shí), Vue.js 專門增強(qiáng)了它。表達(dá)式的結(jié)果類型除了字符串之外,還可以是對象或數(shù)組。
綁定 HTML Class
對象語法
我們可以傳給 v-bind:class 一個(gè)對象,以動(dòng)態(tài)地切換 class 。
<div v-bind:class="{ active: isActive }"></div>
上面的語法表示 classactive 的更新將取決于數(shù)據(jù)屬性 isActive 是否為真值 。
我們也可以在對象中傳入更多屬性用來動(dòng)態(tài)切換多個(gè) 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>
當(dāng) isActive 或者 hasError 變化時(shí),class 列表將相應(yīng)地更新。例如,如果 hasError的值為 true , class列表將變?yōu)?"static active text-danger"。
你也可以直接綁定數(shù)據(jù)里的一個(gè)對象:
<div v-bind:class="classObject"></div>
data: {
classObject: {
active: true,
'text-danger': false
}
}
渲染的結(jié)果和上面一樣。我們也可以在這里綁定返回對象的計(jì)算屬性。這是一個(gè)常用且強(qiáng)大的模式:
<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ù)組語法
我們可以把一個(gè)數(shù)組傳給 v-bind:class ,以應(yīng)用一個(gè) class 列表:
<div v-bind:class="[activeClass, errorClass]">
data: {
activeClass: 'active',
errorClass: 'text-danger'
}
渲染為:
<div class="active text-danger"></div>
如果你也想根據(jù)條件切換列表中的 class ,可以用三元表達(dá)式:
<div v-bind:class="[isActive ? activeClass : '', errorClass]">
此例始終添加 errorClass ,但是只有在 isActive 是 true 時(shí)添加 activeClass 。
不過,當(dāng)有多個(gè)條件 class 時(shí)這樣寫有些繁瑣??梢栽跀?shù)組語法中使用對象語法:
<div v-bind:class="[{ active: isActive }, errorClass]">
With Components
This section assumes knowledge ofVue 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 ,其實(shí)它是一個(gè) JavaScript 對象。 CSS 屬性名可以用駝峰式(camelCase)或短橫分隔命名(kebab-case):
<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
data: {
activeColor: 'red',
fontSize: 30
}
直接綁定到一個(gè)樣式對象通常更好,讓模板更清晰:
<div v-bind:style="styleObject"></div>
data: {
styleObject: {
color: 'red',
fontSize: '13px'
}
}
同樣的,對象語法常常結(jié)合返回對象的計(jì)算屬性使用。
數(shù)組語法
v-bind:style 的數(shù)組語法可以將多個(gè)樣式對象應(yīng)用到一個(gè)元素上:
<div v-bind:style="[baseStyles, overridingStyles]">
自動(dòng)添加前綴
當(dāng) v-bind:style 使用需要特定前綴的 CSS 屬性時(shí),如 transform ,Vue.js 會(huì)自動(dòng)偵測并添加相應(yīng)的前綴。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
查找Vue中下標(biāo)的操作(some和findindex)
這篇文章主要介紹了查找Vue中下標(biāo)的操作(some和findindex),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Avue和Element-UI動(dòng)態(tài)三級表頭的實(shí)現(xiàn)
本文主要介紹了Avue和Element-UI動(dòng)態(tài)三級表頭的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
vue自定義實(shí)例化modal彈窗功能的實(shí)現(xiàn)
這篇文章主要介紹了vue自定義實(shí)例化modal彈窗,Vue.extend 屬于Vue的全局 api,在實(shí)際業(yè)務(wù)開發(fā)中我們很少使用,因?yàn)橄啾瘸S玫?nbsp;Vue.component寫法使用 extend 步驟要更加繁瑣一些,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下2022-09-09
Vue快速實(shí)現(xiàn)通用表單驗(yàn)證的示例代碼
這篇文章主要介紹了Vue快速實(shí)現(xiàn)通用表單驗(yàn)證的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01

