1.class的对象绑定
image.png
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>8.Vue中的样式绑定</title>
<script src="./vue.js"></script>
<style>
.activated {
color: red;
}
</style>
</head>
<body>
<div id="app">
<h1 :class="{activated:isActivated}" @click="handleH1Click">hello world</h1>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
isActivated: true
},
methods: {
handleH1Click() {
this.isActivated = !this.isActivated
}
},
})
</script>
</body>
</html>
2.class和数组绑定,数组的值是变量,class上会显示变量对应的内容.动态的向一个元素添加或者删除不同的类。从而实现页面效果的变更。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>8.Vue中的样式绑定</title>
<script src="./vue.js"></script>
<style>
.activated {
color: red;
}
</style>
</head>
<body>
<div id="app">
<h1 :class="[activated,activatedOne]" @click="handleH1Click">hello world</h1>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
activated: '',
activatedOne:'activated-one'
},
methods: {
handleH1Click() {
this.activated = this.activated === "activated" ? "" : "activated"
}
},
})
</script>
</body>
</html>
3.style改变样式(通过对象或者数组)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>8.Vue中的样式绑定</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="app">
<!-- <h1 :style="styleObj" @click="handleClick">hello world</h1> -->
<h1 :style="[styleObj,{fontSize:'100px'}]" @click="handleClick">hello world</h1>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
styleObj: {
color: "black"
}
},
methods: {
handleClick: function () {
this.styleObj.color = this.styleObj.color = "black" ? "red" : "black"
}
},
})
</script>
</body>
</html>
网友评论