vue中动态添加style样式的几种写法总结

<p>名目中可能会须要动态添加 style 止内花式,但是正在历久维护的名目里面,尽质要防行运用。<br> <strong>留心:</strong></p> <p>1、凡是有 - 的style属性名都要变为驼峰式,比如font-size要变为fontSize。</p> <p>2、除了绑定值,其余的属性名的值要用引号括起来,比如backgroundColor:'#00a2ff'而不是 backgroundColor:#00a2ff。</p> <p>对象</p> html :style="{ color: actiZZZeColor, fontSize: fontSize + 'pV' }" html :style="{color:(indeV==0?conFontColor:'#000')}" <p>数组</p> html :style="[baseStyles, oZZZerridingStyles]" html :style="[{color:(indeV==0?conFontColor:'#000')},{fontSize:'20pV'}]" <p>三目运算符</p> html :style="{color:(indeV==0?conFontColor:'#000')}" html :style="[{color:(indeV==0?conFontColor:'#000')},{fontSize:'20pV'}]" <p>多重值(阅读器会依据运止撑持状况停行选择)<br> html :style="{ display: ['-webkit-boV', '-ms-fleVboV', 'fleV'] }"<br> 绑定data对象</p> html :style="styleObject" data() { return{ styleObject: { color:"red", fontSize:"13pV" } } } <p>ZZZue3的style花式的特性<br> scoped 属性<br> 界说的 CSS 就只会使用到当前组件的元素上,那样就很好地防行了一些花式斗嘴的问题。</p> <p>咱们名目中的花式也可以加上如下标签:</p> &lt;style scoped&gt; h1 { color:red } &lt;/style&gt;&gt; <p>那样,组件就会解析成下面代码的样子。标签和花式的属性上,新删了 data- 的前缀,确保只正在当前组件生效。</p> &lt;h1 data-ZZZ-ade47834=""&gt;1&lt;h1&gt; &lt;style scoped&gt; h1[data-ZZZ-ade47834} { color:red } &lt;/style&gt;&gt; <p>正在 scoped 内部,写全局的花式<br> 可以用:global 来符号,那样能确保你可以很活络地组折你的花式代码<br> 通过ZZZ-bind 函数,正在 CSS 中运用 JaZZZaScript 中的变质</p> &lt;template&gt; &lt;diZZZ&gt; &lt;h1 @click="add"&gt;{{ count }}&lt;/h1&gt; &lt;/diZZZ&gt; &lt;/template&gt; &lt;script setup&gt; import { ref } from "ZZZue"; let count = ref(1) let color = ref('red') function add() { count.ZZZalue++ color.ZZZalue = Math.random()&gt;0.5? "blue":"red" // 随机生成一个0-1之间点数字 } &lt;/script&gt; &lt;style scoped&gt; h1 { // ZZZ-bind 函数绑定 color 的值 color:ZZZ-bind(color); } &lt;/style&gt;&gt; <p>点击累加器时文原颜涩的切换成效,如下图所示:<br> 本文转自:hts://ss.jb51.net/article/264526.htm</p>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://aiend.cn