vue(19):样式绑定

今天复习vue,突然发现关于样式绑定这部分忘记写了,在这里补充一下

vue绑定样式分为两种:class样式和style样式

class样式

对象绑定
格式:v-bind:class="{styleClass:flog}"
styleClass:定义好的样式类
flog:布尔值,表示样式是否显示

实例

<style type="text/css">
	.style1 {
		width: 100px;
		height: 100px;
		border: 1px solid red;
	}

	.style2 {
		background-color: orange;
	}
</style>

<body>
	<div id="app">
		<div :class="{style1:isS1,style2:isS2,....}">vue样式绑定</div>
		<button @click="change">切换样式</button>
	</div>
	<script type="text/javascript">
		new Vue({
			el: '#app',
			data: {
				isS1: true,
				isS2: true
			},
			methods: {
				change() {
					this.isS1 = !this.isS1;
					this.isS2 = !this.isS2;
				}
			}
		})
	</script>
</body>

数组绑定
格式:v-bind:class="[样式变量1,样式变量2,.....]"

实例

<style type="text/css">
	.style1 {
		width: 100px;
		height: 100px;
		border: 1px solid red;
	}

	.style2 {
		background-color: orange;
	}
</style>

<body>
	<div id="app">
		<div :class="[hasBgColor,hasBorder]">vue样式绑定</div>
		<button @click="change">切换样式</button>
	</div>
	<script type="text/javascript">
		new Vue({
			el: '#app',
			data: {
				hasBorder: 'style1',
				hasBgColor: 'style2'
			},
			methods: {
				change() {
					this.hasBgColor = ''
				}
			}
		})
	</script>
</body>

结合使用

<style type="text/css">
	.style1 {
		width: 100px;
		height: 100px;
		border: 1px solid red;
	}

	.style2 {
		background-color: orange;
	}
</style>

<div :class="[hasBgColor,{style2:flog}]">vue样式绑定</div>

new Vue({
	el: '#app',
	data: {
		hasBorder: 'style1',
		flog: true
	},
	methods: {
		change() {
			this.flog = !this.flog
		}
	}
})

style样式

对象绑定
格式:v-bind:style="{样式:值,.......}"

<body>
	<div id="app">
		<div v-bind:style="{border:borderStyle,width:widthStyle,height:heightStyle}">vue样式绑定</div>
	</div>
	<script type="text/javascript">
		new Vue({
			el: '#app',
			data: {
				borderStyle:'1px solid red',
				widthStyle: '100px',
				heightStyle:'100px'
			}
		})
	</script>
</body>

数组绑定
数组绑定以对象绑定为基础,数组元素是一个个样式对象
格式:v-bind:style="[样式1,样式2,...]"

<body>
	<div id="app">
		<div v-bind:style="[baseStyle,fontStyle]">vue样式绑定</div>
	</div>
	<script type="text/javascript">
		new Vue({
			el: '#app',
			data: {
				baseStyle: {
					border: '1px solid red',
					width: '100px',
					height: '100px'
				},
				fontStyle: {
					fontFamily: '宋体',
					fontSize: '20px',
					color:'blue'
				}
			}
		})
	</script>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无知的小菜鸡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值