vue系列文章(7)动态CSS类型绑定

操作元素的 class 列表和内联样式是数据绑定的一个常见需求。因为它们都是属性,所以我们可以用 v-bind 处理它们:只需要通过表达式计算出字符串结果即可。不过,字符串拼接麻烦且易错。因此,在将 v-bind 用于 class 和 style 时,Vue.js 做了专门的增强。表达式结果的类型除了字符串之外,还可以是对象或数组。

 

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Vue.js</title>
	<script src="https://cdn.jsdelivr.net/npm/vue"></script>
	<link rel="stylesheet" type="text/css" href="style.css">
	
</head>
<body>
	<div id="app">
		<h1>动态CSS Class</h1>
		<h2>示例1</h2>
			<div  v-on:click="changeColor = !changeColor" v-bind:class="{changeColor:changeColor}">
				<span>Herry</span>
			</div>
		<button v-on:click="changeColor= !changeColor">change Color</button>
		<button v-on:click="changeLength = !changeLength">changeLength</button>
		<h2>示例2</h2>
		<div v-bind:class="compClass">
			<span>Herry</span>
		</div>
		
	<script type="text/javascript" src="app.js"></script>
</body>
</html>

app.js

new Vue({
	el: '#app',
	data: {
		changeColor: false,
		changeLength: false
	},
	methods: {
		
	},
	computed: {
		compClass: 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-color: green;
}
.changeLength span:after {
	content: "length";
	margin-left: 10px;
}

   Vue系列文章目录 

  1. vue系列文章(1):对象绑定,属性绑定
  2. Vue系列文章(2)事件绑定,鼠标点击事件
  3. vue系列文章(3):事件修饰符
  4. Vue系列文章(4)键盘事件及键盘修饰符
  5. vue系列文章(5)双向数据绑定
  6. vue系列文章(6)计算属性computed
  7. vue系列文章(7)动态CSS类型绑定
  8. vue系列文章(8)条件渲染
  9. vue系列文章(9)v-for条件循环
  10. vue系列文章(10)vue实战项目demo
  11. vue系列文章(11):初始化多个实例对象
  12. vue系列文章(12)初始组件的应用
  13. Vue系列文章(13)vue cli脚手架
  14. vue系列文章(14)vue-cli脚手架,组件嵌套,全局组件注册和局部组件注册
  15. vue系列文章(15)属性传值props
  16. vue系列文章(16)传值和传引用的类型和区别
  17. vue系列文章(17)利用事件传递将子组件值传递给父组件
  18. vue系列文章(18)vue生命周期
  19. vue系列文章(19)vue路由配置
  20. vue系列文章(20) vue网络请求vue-resource

 如果上面文章对你有用,打赏下我吧@*@

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网极客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值