您的位置:首页 > 产品设计 > UI/UE

Vue.js 学习5 Class与Style绑定

2017-06-02 23:19 911 查看

一、绑定Html Class

1.对象语法

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


与普通 class 并存:

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


也可以直接绑定数据里的一个对象

<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',
}
}
}


2.数组语法

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


data: {
activeClass: 'active',
errorClass: 'text-danger'
}


或使用三元表达式:

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


它们等同于对象语法

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


3.用在组件上

定义组件

Vue.component('my-component', {
template: '<p>Hi</p>'
})


<my-component class="baz boo"></my-component>


会被渲染为

<p class="foo bar baz boo">Hi</p>


同样的适用于绑定HTML class

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


4.绑定内联样式

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


data: {
activeColor: 'red',
fontSize: 30
}


css属性名可以用驼峰式(camelCase)或短横分隔命名(kebab-case)。

直接绑定到一个对象上:

<div v-bind:style="styleObject"></div>


data: {
styleObject: {
color: 'red',
fontSize: '13px'
}
}


5.数组语法

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


6.自动添加前缀

7.多重值

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


可以为style绑定中的属性提供一个包含多个值的数组,常用于提供多个带前缀的值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: