Vue入门教程--对元素进行样式绑定详解(article:6)

Vue.js 初步入门


方法简介

  1. 使用class绑定样式。
  2. 使用内联样式style绑定。

使用详情

原生js中,样式的绑定有两种,第一种是直接在标签属性style里赋予自己想自定义的属性:

<div id="container">
    <div style = "color:'blue'">
        {{hello}}
    </div>
</div>
let app = new Vue({
		el:"#container",
		data:{
			hello:"hello vue.js"
		},
		methods:{
			
		}
});

第二种是给标签定义id或者class,然后在样式域给特定的id或class不同的样式:

	<style type="text/css">
		.blue{
			color:blue;
			background-color: #FFC0CB;
		}
		.thin{
			font-weight: 150;
		}
		.size{
			width: 100px;
			height: 100px;
		}
	</style>
<div id="container">
    <div class="blue size">
        {{hello}}
    </div>
</div>
let app = new Vue({
		el:"#container",
		data:{
			hello:"hello vue.js"
		},
		methods:{
			
		}
});

- 使用class绑定样式

vue给用户提供了属性绑定,让用户能够灵活的对class属性进行赋值。

	<style type="text/css">
		.blue{
			color:blue;
			background-color: #FFC0CB;
		}
		.thin{
			font-weight: 150;
		}
		.size{
			width: 100px;
			height: 100px;
		}
	</style>
1.数组
<div :class="['blue','size']">
	{{hello}}
</div>
2. 数组中使用三元表达式
<div :class="['blue','size',isactive?'thin':'']">
	{{hello}}
</div>
data:{
	hello:"hello vue.js",
	isactive:false
}
3.数组中嵌套对象
<div :class="['blue','size',{thin:isactive}]">
	{{hello}}
</div>
4. 直接使用对象
<div :class="{blue:true,thin:false,size:isactive}">
	{{hello}}
</div>

在这里插入图片描述

- 使用class绑定样式
1. 直接在元素上通过:style传入对象进行样式的绑定
<div :style="{color:blue,'font-weight': 150}">
	{{hello}}
</div>
2. 将样式写到数据域中,再引入
data:{
	hello:"hello vue.js",
	isactive:true,
	myStyle1:{color:'blue','font-weight':'150'}
}
<div :style="myStyle1">
	{{hello}}
</div>
3. 将多个样式数据放到数据域中,并用数组引用多个样式对象
data:{
	hello:"hello vue.js",
	isactive:true,
	myStyle1:{color:'blue','font-weight':'150'},
	myStyle2:{'font-size':'40pt',width:'100px',height:'100px'}
}
data:{
	hello:"hello vue.js",
	isactive:true,
	myStyle1:{color:'blue','font-weight':'150'},
	myStyle2:{'font-size':'40pt',width:'100px',height:'100px'}
}

在这里插入图片描述


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值