uniapp动态设置style和class样式

注意:以下两种样式设置,涉及到单位的只能用px,而不能使用rpx

style样式设置

样式名称统一使用驼峰命名方式:例如:fontSize
对象方式

:style="{backgroundImage:`url(${itemB.images})`}"
:style="{ paddingTop: paddingTopNum + 'px' }"

数组加三元运算方式

:class="[items.isShow?'':'noneActive',subIndex[index] == indexs?'value-item-active':'value-item']"

对象数组方式

:style="[{paddingTop: background},{color: getColor}]"
:style="[{ color: activeColor, fontSize: fontSize + 'px' }]"

对象方式

:style="{left:`${iconDistance}rpx`}"

:style="{backgroundImage:`url(${topBgImg})`}"

style方式calc

:style="{height:'calc(100vh - ' + pageTopHeight +'px - 140rpx)'}"

class样式设置

对象方式

:class="{ active: isActive, 'text-danger': hasError }" 

数组方式

:class="[activeClass, errorClass]"

三元运算方式

:class="[isActive ? activeClass : ' ', errorClass]"

数组加对象方式

:class="[{ active: isActive }, errorClass]"
  • 10
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Uniapp中,我们可以使用不同的方式来动态设置`style`属性。其中,常用的方式包括对象数组方式、对象方式和`calc`方式。 1. 对象数组方式:使用`style`属性的对象数组来设置不同的样式属性。例如,可以使用以下方式来设置`paddingTop`和`color`属性: ```html <view :style="[{paddingTop: background},{color: getColor}]"></view> ``` 这里的`background`和`getColor`是对应的变量或方法。 2. 对象方式:使用一个对象来设置`style`属性。例如,可以使用以下方式来设置`left`和`backgroundImage`属性: ```html <view :style="{left:`${iconDistance}rpx`,backgroundImage:`url(${topBgImg})`}"></view> ``` 这里的`iconDistance`和`topBgImg`是对应的变量或方法。 3. `calc`方式:使用`calc`函数来设置`style`属性。例如,可以使用以下方式来设置高度属性: ```html <view :style="{height:'calc(100vh - ' + pageTopHeight + 'px - 140rpx)'}"></view> ``` 这里的`pageTopHeight`是对应的变量或方法。 此外,还可以通过添加`class`样式设置动态样式。例如: ```html <view :class="getClass"></view> ``` 这里的`getClass`是一个返回样式类名的方法。 通过以上方式,我们可以在Uniapp动态设置`style`属性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [uniapp动态设置styleclass样式](https://blog.csdn.net/qq_40666120/article/details/108123186)[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 ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值