Class与Style绑定
Class与Styel都为属性可用v-bind处理它们:过表达式计算出字符串结果但字符串拼接麻烦且易错。因此,在将 v-bind 用于 class 和 style 时表达式结果的类型除了字符串之外,还可以是对象或数组。
绑定HTMLClass
对象语法
传给v-bind:class一个对象,以动态地切换class
<div v-bind:class="{active:isActive}"></div>
active 这个 class 存在与否将取决于数据属性 isActive 的 truthiness。
可以在对象中传入更多属性来动态切换多个 class.此外,v-bind:class 指令也可以与普通的 class 属性共存。当有如下模板:
<div class="static">
v-bind:class="{active:isActive,'text-danger':hasError}"
</div>
data:{
isActive:true,
hasError:false
}
结果渲染为:
<div class="static active"></div>
当 isActive 或者 hasError 变化时,class 列表将相应地更新。例如,如果 hasError 的值为 true,class 列表将变为 “static active text-danger”。
绑定的数据对象不必内联定义在模板里:
<div v-bind:class="classObject"></div>
data:{
classObject:{
active:true,
'text-danger':false
}
}
渲染的结果和上面一样。
也可以在这里绑定一个返回对象的计算属性。这是一个常用且强大的模式:
<div v-bind:class="classObject"></div>
data:{
isActive:true,
error:null
},
computer:{
classObject:function(){
return{
active:this.isActive &&!this.error,
'text-danger':this.error && thiis.error.type == 'fatal'
}
}
}
数组语法
可以把一个数组传给 v-bind:class,以应用一个class列表:
<div v-bind:class="[activeClass,errorClass]"></div>
data:{
activeClass:'active',
errorClass:'text-danger'
}
渲染为
<div class="active text-danger"></div>
如果想根据条件切换列表中的class,可以采用三元表达式:
<div v-bind:class="[isActive ? activeClass : '',errorClass]"></div>
//这样写将始终添加 errorClass,但是只有在 isActive 是 truthy[1] 时才添加 activeClass。
当有多个条件 class 时这样写有些繁琐。所以在数组语法中也可以使用对象语法:
<div v-bind:class="[{active:isActive},errorClass]"></div>
在组件上
当在一个自定义组件上使用 class 属性时,这些类将被添加到该组件的根元素上面。这个元素上已经存在的类不会被覆盖。
例如,你已声明这个组件
vue.component("my-component",{
template:'<p class="foo bar">Hi</p>'
})
然后在使用它的时候添加一些Class
<my-component class="baz boo"><my-component>
HTML将被渲染为
<p class="foo bar baz boo">Hi</p>
对于带数据绑定class也同样适用
<my-component v-bind:class="{active:isActive}"></my-component>
当isActive为turthy时,HTML将会被渲染成
<p class="foo bar active">Hi</p>
绑定内联样式
对象语法
v-bind:style的对象语法看起来非常像Css,但实际是一个Javascript对象。
css属性名可以用驼峰式(classCase)或短横线分隔(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'
}
}
同样的,对象语法常常结合返回对象的计算属性使用。
数组语法
v-bind:style 的数组语法可以将多个样式对象应用到同一个元素上
<div v-bind:style="[baseStyle,overridingStyles]"></div>
自动添加前缀
当v-bind:style使用需要添加浏览器引擎前缀的CSS属性时,如transfrom,vue.js会自动侦测并添加响应的前缀
多重值
从 2.3.0 起你可以为 style 绑定中的属性提供一个包含多个值的数组,常用于提供多个带前缀的值,例如:
<div :style="{display:['-webkit-box','-ms-flexbox','flex']}"></div>
*这样写只会渲染数组中最后一个被浏览器支持的值。在本例中,如果浏览器支持不带浏览器前缀的 flexbox,那么就只会渲染 display: flex*
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<style>
.active{
width:100px;
height:100px;
background:green;
}
.active2{
width:100px;
height:100px;
background:blue;
}
.text-danger{
background:red;
}
.active3{
width:100px;
height:100px;
background:green;
}
.text-danger{
background:red;
}
.active4{
width:100px;
height:100px;
background:green;
}
.text-danger4{
background:red;
}
</style>
<body>
<div id="app1">
<div v-bind:class="{active: isActive}"></div>
</div>
<div id="app2">
<div class="static" v-bind:class="{ active2: isActive2,'text-danger':hasError}">
</div>
</div>
<div id="app3">
<div v-bind:class="classObject">
</div>
</div>
<div id="app4">
<div v-bind:class="classObject4">
</div>
</div>
<div id="app5">
<div v-bind:class="[activeClass,errorClass]">
</div>
</div>
<div id="app6">
<div v-bind:class="[errorClass,isActive ? activeClass : ' ']">
</div>
</div>
<div id="app7">
<div v-bind:style="{color:activeColor,fontSize:fontSize + 'px' }">
菜鸟教程
</div>
</div>
<div id="app8">
<div v-bind:style="styleObject">
菜鸟教程
</div>
</div>
<div id="#app9">
<div v-bind:style="[baseStyle,overridingStyles]">
菜鸟教程
</div>
</div>
</body>
</html>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
<script type="text/javascript">
new Vue({
el:'#app1',
data:{
isActive:true
}
});
new Vue({
el:'#app2',
data:{
isActive2:true,
hasError:true
}
});
new Vue({
el:'#app3',
data:{
classObject:{
active3:true,
'text-danger':true
}
}
});
new Vue({
el:'#app4',
data:{
isActive:true,
error:null
},
computed:{
classObject4:function(){
return{
active4:this.isActive && ! this.error,
'text-danger':this.error && this.error.type == 'fatal',
}
}
}
});
new Vue({
el:'#app5',
data:{
activeClass:'active',
errorClass:'text-danger'
}
});
new Vue({
el:'#app6',
data:{
isActive:true,
activeClass:'active',
errorClass:'text-danger'
}
});
new Vue({
el:'#app7',
data:{
activeColor:'green',
fontSize:30
}
});new Vue({
el:'#app8',
data:{
styleObject:{
color:'greeen',
fontSize:'30px'
}
}
});
new Vue({
el:"#app9",
data:{
baseStyles:{
color:'green',
fontSize:'30px'
},
overridingStyles:{
'font-weight':'bold'
}
}
});
</script>