绑定class属性
想要动态的设置class,也是给一个对象
属性名:就是类名
属性值是布尔值,如果给true,代表有这个类目,如果给false,代表没有这个类名
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
width: 300px;
height: 200px;
border: 10px solid aquamarine;
margin: 0 auto;
}
.bg{
background-color: bisque;
}
</style>
</head>
<body>
<div id="app">
<div class="box" v-bind:class="{bg:isBg}">
<div>
<button @click="btn">更改背景颜色</button>
</div>
</div>
</div>
<script src="../js/vue.js"></script>
<script>
new Vue({
el:'#app',
data:{
isBg:true
},
methods:{
btn(){
this.isBg='red'
}
}
})
</script>
</body>
</html>
绑定style属性
直接上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<div style="color: aqua; font-size: 55px;">晚点</div>
<!-- 把样式属性改造成对象属性 :
要用大括号包裹起来;
把样式转换成属性值,要用引号引起来;
把分号改成逗号
样式名转换成对象的属性值-->
<div v-bind:style="{color:'aqua',fontSize:'55px'}">晚点</div>
<!-- 把对象值改变成变量 -->
<div v-bind:style="{color:color,fontSize:fontSize}">晚点</div>
</div>
<script src="../js/vue.js"></script>
<script>
new Vue({
el: '#app',
data:{
color:'blue',
fontSize:'56px'
}
})
</script>
</body>
</html>