vue.js Class 与 Style 绑定(vue 三)

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

绑定 HTML Class

对象语法

我们可以传给 v-bind:class 一个对象,以动态地切换 class:

<div v-bind:class="{ active: isActive }"></div>

你可以在对象中传入更多属性来动态切换多个 class。此外,v-bind:class 指令也可以与普通的 class 属性共存。当有如下模板:

<div class="static"
     v-bind:class="{ active: isActive, 'text-danger': hasError }">
</div>

结果渲染为:

<div class="static active"></div>

当 isActive 或者 hasError 变化时,class 列表将相应地更新。例如,如果 hasError 的值为 true,class 列表将变为 “static active text-danger”。

绑定的数据对象不必内联定义在模板里:

<div v-bind:class="classObject"></div>
data: {
  classObject: {
    active: true,
    'text-danger': false
  }
}

渲染的结果和上面一样。我们也可以在这里绑定一个返回对象的计算属性。这是一个常用且强大的模式:

<div v-bind:class="classObject"></div>
data: {
  isActive: true,
  error: null
},
computed: {
  classObject: function () {
    return {
      active: this.isActive && !this.error,
      'text-danger': this.error && this.error.type === 'fatal'
    }
  }
}
数组语法

我们可以把一个数组传给 v-bind:class,以应用一个 class 列表:

<div v-bind:class="[activeClass, errorClass]"></div>
data: {
  activeClass: 'active',
  errorClass: 'text-danger'
}

渲染为:

<div class="active text-danger"></div>
完成代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Class 与 Style 绑定</title>
<style>
.active {
	width: 100px;
	height: 100px;
	background: green;
}

.danger {
	background: red;
}

.warn {
	background: yellow;
}


</style>
</head>
<body>
	<div id="app">
		<div v-bind:class="{active: isActive, danger:hasError}"></div>
		<div v-bind:class="classObject"></div>
		<div v-bind:class="classMethod"></div>
		<div v-bind:class="[activeClass, errorClass]"></div>
	</div>
</body>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
	var vm = new Vue({
		el : '#app',
		data : {
			isActive : true,
			hasError : true,
			classObject : {
				active : true,
				hasError : false
			},
			activeClass : 'active',
			errorClass : 'warn'
		},
		computed : {
			classMethod : function() {
				return {
					active : true,
					danger : true
				}
			}
		}
	});
</script>
</html>

测试结果
在这里插入图片描述

绑定内联样式class

对象语法

v-bind:style 的对象语法十分直观——看着非常像 CSS,但其实是一个 JavaScript 对象。CSS 属性名可以用驼峰式 (camelCase) 或短横线分隔 (kebab-case,记得用单引号括起来) 来命名:

<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
data: {
  activeColor: 'red',
  fontSize: 30
}

直接绑定到一个样式对象通常更好,这会让模板更清晰:

<div v-bind:style="styleObject"></div>
data: {
  styleObject: {
    color: 'red',
    fontSize: '13px'
  }
}
数组语法

v-bind:style 的数组语法可以将多个样式对象应用到同一个元素上:

<div v-bind:style="[baseStyles, overridingStyles]"></div>
完整例子
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Class 与 Style 绑定</title>
<style>
.active {
	width: 100px;
	height: 100px;
	background: green;
}

.danger {
	background: red;
}
</style>
</head>
<body>
	<div id="app">
		<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }">1243abc</div>
		<div v-bind:style="styleObject">hehhe</div>
		<div v-bind:style="[baseStyles, overridingStyles]">1234</div>
	</div>
</body>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
	var vm = new Vue({
		el : '#app',
		data : {
			activeColor : 'red',
			fontSize : 30,
			styleObject : {
				color : 'blue',
				fontSize : '40px'
			},
			baseStyles : {
				color : 'black'
				
			},
			overridingStyles : {
				fontSize : '20px'
			}
		}
	});
</script>
</html>

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值