VUE 学习(六)样式绑定v-bind:class和v-bind:style

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>VUE学习-样式绑定</title>
		<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
		<style>
			.active{
				width: 100px;
				height: 100px;
				background-color: aqua;
			}
			.text-danger {
				background: red;
			}
			
			.base {
			  	width: 100px;
			  	height: 100px;
			}
			
			.text-danger1 {
				width: 100px;
				height: 100px;
				background: red;
			}
			
			.active1 {
			  	background: aqua;
			}
			.active2 {
				width: 100px;
				height: 100px;
			  	background: yellow;
			}
			
			
		</style>
	</head>
	<body>
		<!--class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。
		Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。-->
		
		<!--我们可以为 v-bind:class 设置一个对象,从而动态的切换 class:-->
		<!--例1:-->
		<div id="div1">
			<div v-bind:class="{'active':'isActive'}"></div>
		</div>
		<br /><br />
		<!--以上实例 div class 为:<div class="active"></div>-->
		
		
		
		
		<!--我们也可以在对象中传入更多属性用来动态切换多个 class 。-->
		<!--text-danger 类背景颜色覆盖了 active 类的背景色:-->
		<!--例2:-->
		<div id="div2">
		  	<div class="static" v-bind:class="{ 'active': isActive, 'text-danger': hasError }">
		  	</div>
		</div>
		<br /><br />
		<!--以上实例 div class 为:<div class="static active text-danger"></div>-->
		
		
		
		<!--我们也可以直接绑定数据里的一个对象:-->
		<!--text-danger 类背景颜色覆盖了 active 类的背景色:-->
		<!--例3:-->
		<div id="div3">
			<div v-bind:class="classObject"></div>
		</div>
		<br /><br />
		<!--实例 3 与 实例 2 的渲染结果是一样的。-->
		
		
		
		<!--此外,我们也可以在这里绑定返回对象的计算属性。这是一个常用且强大的模式:-->
		<!--例4:-->
		<div id="div4">
			<div v-bind:class="classObject"></div>
		</div>
		<br /><br />
		
		
		
		<!--我们可以把一个数组传给 v-bind:class ,实例如下:-->
		<!--例5:-->
		<div id="div5">
			<div v-bind:class='[activeclass,dangerclass]'></div>
		</div>
		<br /><br />
		<!--以上实例 div class 为:<div class="active text-danger"></div>-->
		
		
		
		<!--我们还可以使用三元表达式来切换列表中的 class :-->
		<!--errorClass 是始终存在的,isActive 为 true 时添加 activeClass 类,isActive 为 false 时添加 activeclasser 类:-->
		<!--例6:-->
		<div id="div6">
			<div v-bind:class="[errorClass,isActive ? activeClass : activeclasser]"></div>
		</div>
		<br /><br />
		
		
		<!--我们可以在 v-bind:style 直接设置样式:-->
		<!--例7:-->
		<div id="div7">
			<div v-bind:style="{color:activecolor,fontSize:fontSize+'px'}">你好小爱同学</div>
		</div>
		<br /><br />
		<!--以上实例 div style 为:<div style="color: aqua; font-size: 20px;">你好小爱同学</div>-->
		
		
		
		<!--也可以直接绑定到一个样式对象,让模板更清晰:-->
		<!--例8:-->
		<div id="div8">
			<div v-bind:style='styleobject'>小爱同学来首音乐</div>
		</div>
		<br /><br />
		
		
		
		
		<!--v-bind:style 可以使用数组将多个样式对象应用到一个元素上:-->
		<!--例9:-->
		<div id="div9">
			<div v-bind:style="[baseStyles,overridingStyles]">小姐姐谈对象嘛?挖心的那种</div>
		</div>
		<script>
			new Vue({
				el:'#div1',
				data:{
					isActive:true
				}
			});
			new Vue({
				el:'#div2',
				data:{
					isActive:true,
					hasError:true
				}
			});
			new Vue({
				el:'#div3',
				data:{
					classObject:{
						active:true,
						'text-danger':true
					}
				}
			});
			new Vue({
				el:'#div4',
				data:{
					isActive: true,
				    error: {
				      value: true,
				      type: 'fatal'
				    }
				},
				computed:{
					classObject:function(){
						return{
							base:true,
							active1:this.isActive && !this.error.value,
							'text-danger':this.error.value && this.error.type=='fatal'
						}
					}
				}
			});
			new Vue({
				el:'#div5',
				data:{
					activeclass:'active',
					dangerclass:'text-danger'
				}
			});
			new Vue({
			  el: '#div6',
			  data: {
			    isActive: false,
				activeClass: 'active1',
				activeclasser: 'active2',
			    errorClass: 'text-danger1'
			  }
			});
			new Vue({
				el:"#div7",
				data:{
					activecolor:'aqua',
					fontSize:20
				}
			});
			new Vue({
				el:'#div8',
				data:{
					styleobject:{
						color:'aqua',
						fontSize:'20px'
					}
				}
			});
			new Vue({
				el:'#div9',
				data:{
					baseStyles:{
						color:'white',
						fontSize:'20px'
					},
					overridingStyles:{
						'background-color':'black'
					}
				}
			});
		</script>
	</body>
</html>

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

豆皮没有豆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值