Vue.2.0.5實現(xiàn)Class 與 Style 綁定的實例
Class 與 Style 綁定
數(shù)據(jù)綁定一個常見需求是操作元素的 class 列表和它的內聯(lián)樣式。因為它們都是屬性 ,我們可以用v-bind 處理它們:只需要計算出表達式最終的字符串。不過,字符串拼接麻煩又易錯。因此,在 v-bind 用于 class 和 style 時, Vue.js 專門增強了它。表達式的結果類型除了字符串之外,還可以是對象或數(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 列表將相應地更新。例如,如果 hasError的值為 true , class列表將變?yōu)?"static active text-danger"。
你也可以直接綁定數(shù)據(jù)里的一個對象:
<div v-bind:class="classObject"></div>
data: { classObject: { active: true, 'text-danger': false } }
渲染的結果和上面一樣。我們也可以在這里綁定返回對象的計算屬性。這是一個常用且強大的模式:
<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 ,以應用一個 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 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>
綁定內聯(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' } }
同樣的,對象語法常常結合返回對象的計算屬性使用。
數(shù)組語法
v-bind:style 的數(shù)組語法可以將多個樣式對象應用到一個元素上:
<div v-bind:style="[baseStyles, overridingStyles]">
自動添加前綴
當 v-bind:style 使用需要特定前綴的 CSS 屬性時,如 transform ,Vue.js 會自動偵測并添加相應的前綴。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue組件傳值異步問題子組件拿到數(shù)據(jù)較慢解決
這篇文章主要為大家介紹了Vue組件傳值異步中子組件拿到數(shù)據(jù)較慢的問題解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08前端vue框架select下拉數(shù)據(jù)量過大造成卡頓問題解決辦法
這篇文章主要給大家介紹了關于前端vue框架select下拉數(shù)據(jù)量過大造成卡頓問題解決辦法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用select具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07vue3+springboot部署到Windows服務器的詳細步驟
這篇文章主要介紹了vue3+springboot部署到Windows服務器,配置Nginx時,因為現(xiàn)在是把vue前端交給了Nginx代理,所以這里的端口號不一定是我們在vue項目中設置的端口號,本文給大家介紹的非常詳細,需要的朋友參考下吧2022-10-10vite+vue3不清除瀏覽器緩存直接下載最新代碼的解決方案
vite+vue3項目發(fā)布后,瀏覽器上還是舊代碼,沒有及時更新到最新代碼,下面通過本文給大家分享vite+vue3不清除瀏覽器緩存直接下載最新代碼的解決方案,感興趣的朋友一起看看吧2024-06-06