2021-08-01 总结:v-bind绑定样式的五种方式

本文总结了Vue中v-bind绑定样式的五种方法:1.使用数组绑定多个class;2.通过数组包含对象绑定class;3.数组包含方法动态绑定class;4.数组绑定多个style;5.通过数组包含方法绑定style。每种方法都有详细的代码示例和运行结果展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总结

1.数组绑定多个class

html: :class="['yanse','ziti']"
css: .yanse{}  .ziti{}

2.数组包含对象绑定class

html: :class="[{yanse:flag},'ziti']"
js>data: flag:true/false

3.数组包含方法绑定class

html: :class="[biankuangShow(),'ziti']"
js>methods: biankuangShow(){}

4.数组绑定多个style

html: :style="[ysStyle,ztStyle]"
js>data: ysStyle:{},ztStyle:{}

5.数组包含方法绑定style

html: :style="[ysStyle,zitiShow()]"
js>methods: zitiShow()

1.class方式

1.1.数组绑定多个class
例子
代码
<div id="app">
	<div v-bind:class="['yanse','ziti']">class 数组绑定多个class</div>
</div>
<script>
	new Vue({el:"#app"});
</script>
<style>
	.yanse{
		color:yellow;
		background:red;
	}
	.ziti{
		font-size:20px;
		font-weight:900
	}
</style>
结果:

在这里插入图片描述

1.2.数组包含对象绑定class
例子
代码
<div id="app">
	<div v-bind:class="[{yanse:flag},'ziti']">class 数组包含对象绑定</div>
</div>
<script>
	new Vue({
		el:"#app",
		data:{
			flag:true
			// flag:false
			},
	});
</script>
<style>
	.yanse{
		color:yellow;
		background:red;
	}
	.ziti{
		font-size:20px;
		font-weight:900
	}
</style>
结果

1.flag:true
在这里插入图片描述
2.flag:false
在这里插入图片描述

1.3.数组包含方法绑定class
例子
代码
<div id="app">
	<div v-bind:class="[biankuangShow(),'ziti']">class 数组包含方法绑定</div>
</div>
<script>
	new Vue({
		el:"#app",
		methods:{
			biankuangShow(){
				return'styleborder'
			}
		}
	});
</script>
<style>
	.styleborder{
		border:5px dotted gold;
	}
	.ziti{
		font-size:20px;
		font-weight:900
	}
</style>
结果:

在这里插入图片描述

2.style方式

2.1.数组绑定多个style
例子
代码
<div id="app">
	<div v-bind:style="[ysStyle,ztStyle]">style 数组绑定多个style</div>
</div>
<script>
	new Vue({
		el:"#app",
		data:{
			ysStyle:{
				color:"orange",
				background:"pink"
			},
			ztStyle:{
				fontSize:"20px",
				fontWeight:"900"
			}
		}	
	});
</script>
结果

在这里插入图片描述

2.2.数组包含方法绑定style
例子
代码
<div id="app">
	<div v-bind:style="[ysStyle,zitiShow()]">style 数组包含方法绑定</div>
</div>
<script>
	new Vue({
		el:"#app",
		data:{
			ysStyle:{
				color:"orange",
				background:"pink"
			}
		},
		methods:{
			zitiShow(){
				return {
					fontSize:"20px",
					fontWeight:"900"
				}
			}
		}
	});
</script>
结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

前端OnTheRun

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

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

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

打赏作者

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

抵扣说明:

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

余额充值