样式绑定分两种:
- v-bind:class =“”
- v-bind:style=“”
表达式的结果可以是数组也可以是对象,但是都是vue中的变量
直接定义class的名字则不生效
一:对象赋值
<html>
<head>
<style>
.active{
width:100px;
height:100px;
backgroud:Red;
}
</style>
</head>
<body>
<div id="app">
<div v-bind:class="{active:isture}"></div>
</div>
<script>
new Vue({
el:"#app",
data:{
istrue:true
}
})
</script>
</body>
</html>
运行结果:
v-bind:class=对象
对象既:active=【键】 istrue=【值】
也可以绑定多个对象:
<div v-bind:class="{active:istrue,isround}"></div>
<script>
new Vue({
el:"#app",
data:{
istrue:true,
isround:true
}
})
</script>
绑定多个参数可以进行切换效果
二:变量赋值
<style>
.active{width:100px;height:100px;background:Red;}
</style>
<div v-bind:class="myclass"></div>
<script>
new Vue({
el:"#app",
data:{
myclass:"active"
}
})
</script>
运行结果和上图一致!
三:构造一个对象返回,其实还是对象赋值
1.html 部分
<style>
.active{width:100px;height:100px;}
.mcolor{backgroud:green;}
</style>
<div v-bind:class="objclass"></div>
Vue代码部分
new Vue({
el:"#app",
data:{
},
computed:{
objclass:function(){
return:{
active:true,
mcolor:true,
}
}
}
})
运行结果如图:
所以 如果用对象的表达则是
【键】:【值】
【键】=classname
【值】=判断运算值 true false