Vue动态设置Style属性

 :style="{ color:domain.groups == 1? '#ccc': domain.groups == 2 ? '#ccc' : '' }"
凡是有-的style属性名都要变成驼峰式,比如font-size要变成fontSize
除了绑定值,其他的属性名的值要用引号括起来,比如backgroundColor:'#00a2ff'
而不是 backgroundColor:#00a2ff    
对象
html :style="{ color: activeColor, fontSize: fontSize + 'px' }"
html :style="{display:(activeName=='first'?'flex':'none')}"
数组
html :style="[baseStyles, overridingStyles]"
html :style="[{display:(activeName=='first'?'flex':'none')},{fontSize:'20px'}]"
三目运算符
html :style="{color:(index==0?conFontColor:'#ddd')}"
html :style="[{color:(index==0?conFontColor:'#ddd')},{fontSize:'22px'}]"
多重值
此时,浏览器会根据运行支持情况进行选择
html :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中可以通过多种方式动态设置样式。一种方式是使用三目运算符形式,通过条件判断来设置样式。例如,可以使用`:style="{background:index===0 ? '#FFFFFF' : '#000000'}"`来根据条件设置背景颜色。另一种方式是使用绑定计算属性,通过计算属性返回样式字符串来设置样式。例如,可以使用`:style="setIconStyle"`,其中`setIconStyle`是一个计算属性,返回样式字符串。还可以通过在模板中绑定一个数组来动态设置样式,其中数组中的元素是对象,对象的key是CSS属性名,value是对应的值。例如,可以使用`:style="\[baseStyles, activeStyles\]"`来合并多个样式对象。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [Vue动态设置Style属性](https://blog.csdn.net/weixin_42120669/article/details/120572179)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue动态绑定style样式之动态添加style样式的多种写法](https://blog.csdn.net/weixin_43743175/article/details/125274617)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Vue动态样式方法总结](https://blog.csdn.net/2301_76615440/article/details/129902737)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值