<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>VUE学习-样式绑定</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<style>
.active{
width: 100px;
height: 100px;
background-color: aqua;
}
.text-danger {
background: red;
}
.base {
width: 100px;
height: 100px;
}
.text-danger1 {
width: 100px;
height: 100px;
background: red;
}
.active1 {
background: aqua;
}
.active2 {
width: 100px;
height: 100px;
background: yellow;
}
</style>
</head>
<body>
<!--class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。
Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。-->
<!--我们可以为 v-bind:class 设置一个对象,从而动态的切换 class:-->
<!--例1:-->
<div id="div1">
<div v-bind:class="{'active':'isActive'}"></div>
</div>
<br /><br />
<!--以上实例 div class 为:<div class="active"></div>-->
<!--我们也可以在对象中传入更多属性用来动态切换多个 class 。-->
<!--text-danger 类背景颜色覆盖了 active 类的背景色:-->
<!--例2:-->
<div id="div2">
<div class="static" v-bind:class="{ 'active': isActive, 'text-danger': hasError }">
</div>
</div>
<br /><br />
<!--以上实例 div class 为:<div class="static active text-danger"></div>-->
<!--我们也可以直接绑定数据里的一个对象:-->
<!--text-danger 类背景颜色覆盖了 active 类的背景色:-->
<!--例3:-->
<div id="div3">
<div v-bind:class="classObject"></div>
</div>
<br /><br />
<!--实例 3 与 实例 2 的渲染结果是一样的。-->
<!--此外,我们也可以在这里绑定返回对象的计算属性。这是一个常用且强大的模式:-->
<!--例4:-->
<div id="div4">
<div v-bind:class="classObject"></div>
</div>
<br /><br />
<!--我们可以把一个数组传给 v-bind:class ,实例如下:-->
<!--例5:-->
<div id="div5">
<div v-bind:class='[activeclass,dangerclass]'></div>
</div>
<br /><br />
<!--以上实例 div class 为:<div class="active text-danger"></div>-->
<!--我们还可以使用三元表达式来切换列表中的 class :-->
<!--errorClass 是始终存在的,isActive 为 true 时添加 activeClass 类,isActive 为 false 时添加 activeclasser 类:-->
<!--例6:-->
<div id="div6">
<div v-bind:class="[errorClass,isActive ? activeClass : activeclasser]"></div>
</div>
<br /><br />
<!--我们可以在 v-bind:style 直接设置样式:-->
<!--例7:-->
<div id="div7">
<div v-bind:style="{color:activecolor,fontSize:fontSize+'px'}">你好小爱同学</div>
</div>
<br /><br />
<!--以上实例 div style 为:<div style="color: aqua; font-size: 20px;">你好小爱同学</div>-->
<!--也可以直接绑定到一个样式对象,让模板更清晰:-->
<!--例8:-->
<div id="div8">
<div v-bind:style='styleobject'>小爱同学来首音乐</div>
</div>
<br /><br />
<!--v-bind:style 可以使用数组将多个样式对象应用到一个元素上:-->
<!--例9:-->
<div id="div9">
<div v-bind:style="[baseStyles,overridingStyles]">小姐姐谈对象嘛?挖心的那种</div>
</div>
<script>
new Vue({
el:'#div1',
data:{
isActive:true
}
});
new Vue({
el:'#div2',
data:{
isActive:true,
hasError:true
}
});
new Vue({
el:'#div3',
data:{
classObject:{
active:true,
'text-danger':true
}
}
});
new Vue({
el:'#div4',
data:{
isActive: true,
error: {
value: true,
type: 'fatal'
}
},
computed:{
classObject:function(){
return{
base:true,
active1:this.isActive && !this.error.value,
'text-danger':this.error.value && this.error.type=='fatal'
}
}
}
});
new Vue({
el:'#div5',
data:{
activeclass:'active',
dangerclass:'text-danger'
}
});
new Vue({
el: '#div6',
data: {
isActive: false,
activeClass: 'active1',
activeclasser: 'active2',
errorClass: 'text-danger1'
}
});
new Vue({
el:"#div7",
data:{
activecolor:'aqua',
fontSize:20
}
});
new Vue({
el:'#div8',
data:{
styleobject:{
color:'aqua',
fontSize:'20px'
}
}
});
new Vue({
el:'#div9',
data:{
baseStyles:{
color:'white',
fontSize:'20px'
},
overridingStyles:{
'background-color':'black'
}
}
});
</script>
</body>
</html>