在这一片博客里面主要讲解了如何通过@:click也就是(v-on:click)来绑定css样式进行动态控制
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<!--vue-app是根容器-->
<div id="vue-app">
<h1>动态 css Class</h1>
<h2>示例 1</h2>
<!-- <div
v-on:click="changeColor = !changeColor"
v-bind:class="{changeColor:changeColor}">
<span>Henry</span>
</div> -->
<h2>示例 2</h2>
<button @click="changeColor = !changeColor">change Color</button>
<button @click="changeLength = !changeLength">change Length</button>
<div v-bind:class="compClasses" >
<span>Henry</span>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
app.js
//实例化VUE对象
new Vue({
el:"#vue-app",
//仅限于在vue-app容器下
data:{
changeColor:false,
changeLength:false
},
methods:{
},
/*
做搜索或者类似的功能的时候
用计算属性可以很好的优化我们的搜索
节省我们的时间
*/
computed:{
compClasses:function(){
return {
changeColor:this.changeColor,
changeLength:this.changeLength
}
},
}
});
style.css
span{
background: red;
display: inline-block;
padding: 10px;
color: #fff;
margin: 10px 0;
}
.changeColor span{
background: green;
}
.changeLength span:after{
content: "length";
margin-left: 10px;
}