vue-Class 與 Style 綁定

1.綁定 HTML  classweb

#對象語法數組

<div class="static"
v-bind:class="{ active: isActive, 'text-danger': hasError }">
</div>
data: {
isActive: true,
hasError: false
}
結果渲染爲:
<div class="static active"></div>

or
<div v-bind:class="classObject"></div>
data: {
classObject: {
active: true,
'text-danger': false
}
}
or:使用計算屬性
<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'
}
}
}

#數組語法
<div v-bind:class="[activeClass, errorClass]"></div>
data: {
activeClass: 'active',
errorClass: 'text-danger'
}
or
<div v-bind:class="[{ active: isActive }, errorClass]"></div>

2.綁定style

#對象語法

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

#數組語法
<div v-bind:style="[baseStyles, overridingStyles]"></div>

#自動添加前綴

當  使用須要添加瀏覽器引擎前綴的 CSS 屬性時,如 ,Vue.js 會自動偵測並添加相應的前綴。

#多重值

從 2.3.0 起你能夠爲  綁定中的屬性提供一個包含多個值的數組,經常使用於提供多個帶前綴的值,例如:

<div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"></div>

這樣寫只會渲染數組中最後一個被瀏覽器支持的值。在本例中,若是瀏覽器支持不帶瀏覽器前綴的 flexbox,那麼就只會渲染 。
v-bind:styletransformstyledisplay: flex
相關文章
相關標籤/搜索