uniapp、vue中动态添加绑定style、class

一、动态设置style

1.普通对象动态添加(比较常见)

<template>
	<view>
		<view :style="{color:fontColor}"> </view>
		 
		<view :style="{ paddingTop: num + 'px' }"></view>
		 
		<view :style="{backgroundImage: 'url(' + imageURL + ')','background-repeat':'no-repeat', 
		         backgroundSize:'100% 100%'}"></view>
	
		//1.动态添加颜色
		//2.动态添加边距
		//3.动态添加背景图片
	</view>
</template>
 
<script>
	export default {
			data() {
				return {
					imageURL: 'https://app-file.beitaichufang.com/img/H5/web/banner/banner23.jpg', //图片
					num:20, //字体大小
					fontColor:'red' //字体颜色
				}
		  }
	}
</script>

2.数组对象动态添加

<template>
	<view>
		<view :style="[{paddingTop: num + 'px'},{color:fontColor}]"></view>    
		
		<view :style="[{'background-image':`url(${imageURL})`},{'background-repeat':'no-repeat'},
		              {'background-size':'100% 100%'}]"></view>
		
		//1.动态添加颜色,动态添加边距
		//2.动态添加背景图片
	</view>
</template>
 
<script>
	export default {
			data() {
				return {
					imageURL: 'https://app-file.beitaichufang.com/img/H5/web/banner/banner23.jpg', //图片
					num:20, //字体大小
					fontColor:'red' //字体颜色
				}
		  }
	}
</script>

3.三目运算动态添加

<template>
	<view>
		<view :style="[{color:(flag?fontColor:'green')},{fontSize:'20px'}]">green</view>
		
		<view :style="[{color:(!flag?fontColor:'green')}]">red</view>
		
		//如果flag为true   颜色就为red色
		//如果flag为false  颜色就为green色
 
 
       <view :style="[flag?{top:searchTop + 'px',width:searchWidth + 'px'}:{top:'100px',width:'100%'}]"></view>
	</view>
</template>
 
<script>
   export default {
		data() {
			return {
				fontColor:'red',
				flag:false,
                searchTop:20,
                searchWidth:100
			}
	  }
}
</script>

二、动态设置 class

<template>
     <view :class="flag?'flaGreen':'flagRed'">I's Xq</view>
</template>
 
<script>
   export default {
		data() {
			return {
				flag:false
			}
	  }
}
</script>
 
<style>
	.flaGreen{
		color: green
	}
	.flagRed{
		color: red
	}
</style>

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
uniapp,可以使用动态style来实现样式的动态绑定。有几种方法可以实现这个目的。 首先,可以使用三元表达式来改变整个style对象。比如,可以使用以下代码来动态绑定view的样式: ```html <view :style="isActive ? activeStyle : viewStyle"></view> ``` 其,isActive是一个布尔值,activeStyle和viewStyle是两个样式对象,根据isActive的值来选择应用哪个样式对象。 其次,如果只想动态绑定样式的某一个属性,可以使用以下代码实现: ```html <view :style="{height: myHeight}"></view> ``` 在data定义了一个属性myHeight,可以在模板通过对象语法将其绑定到view的height属性上。 在Vue模板动态绑定样式时需要使用对象语法,可以将样式对象放在一个数组动态绑定。正确的写法如下: ```html <view :style="[isActive ? activeStyle : viewStyle"></view> ``` 这样可以确保样式对象被正确地解析和应用。 综上所述,以上是uniapp实现动态style的几种方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [uniapp使用三元运算符动态绑定元素的style样式](https://blog.csdn.net/qq_43671018/article/details/131959899)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值