vue动态绑定style样式写法(教你绑定style属性的5种方法)

vue动态绑定style样式写法,要注意:

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

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

vue动态绑定style属性的5种方法如下:


一、对象方法

  • 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'
      }  
    }
}

版权声明:
作者:Joker 链接:https://hooper.eu.org/archives/15194
文章版权归作者所有,转载请注明出处。
THE END
分享
二维码
打赏
< <上一篇
下一篇>>