vue之动态添加style样式的几种写法

项目中可能会需要动态添加 style 行内样式,但是在长期维护的项目里面,尽量要避免使用。

注意:

1、凡是有 - 的style属性名都要变成驼峰式,比如font-size要变成fontSize。

2、除了绑定值,其他的属性名的值要用引号括起来,比如backgroundColor:'#00a2ff'而不是 backgroundColor:#00a2ff。

对象

html :style="{ color: activeColor, fontSize: fontSize + 'px' }"

html :style="{color:(index==0?conFontColor:'#000')}"

数组

html :style="[baseStyles, overridingStyles]"

html :style="[{color:(index==0?conFontColor:'#000')},{fontSize:'20px'}]"

三目运算符

html :style="{color:(index==0?conFontColor:'#000')}"

html :style="[{color:(index==0?conFontColor:'#000')},{fontSize:'20px'}]"

多重值(浏览器会根据运行支持情况进行选择)

html :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }"

绑定data对象

html :style="styleObject"

data() {
    return{
      styleObject: {
        color: 'red',
        fontSize: '13px'
      }  
    }
}

猜你喜欢

转载自blog.csdn.net/u012320487/article/details/121807465
今日推荐