当前位置:网站首页>Dynamic style binding --style and class
Dynamic style binding --style and class
2022-06-30 20:35:00 【Mr. Liu works hard】
One : dynamic style How to write it
Note before writing :
All have - Of style Attribute names have to be humped , such as font-size To become fontSize
Except binding value , The values of other attribute names should be enclosed in quotation marks , for example
backgroundColor:#00a2ff To write backgroundColor:‘#00a2ff’.
One 1 object form
When we don't create separate css When you file , Write... Directly in the code
<div :style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
<div :style="{color:(index == 1 ? conFontColor:'#000')}"></div>
perhaps
<div
:style="{
boxSizing:'border-box',
height:29+'px',
top:(valch-1)*29+'px',
background:chose(val.subjectCode),
borderLeft:' 10px ' + ' solid ' + chose1(val.subjectCode
)}" > /<div>
One 2 Array form
<div :style="[baseStyles, overridingStyles]"></div>
<div :style="[{color:(index == 1 ? conFontColor:'#000')},{fontSize:'18px'}]"></div>
One 3 Ternary operator
<div :style="{color:(index == 1 ? conFontColor:'#000')}"></div>
<div :style="[{color:(index == 1 ? conFontColor:'#000')},{fontSize:'18px'}]"></div>
<div
:style="item.layerName === activeLayerName?'font-weight:700' : 'font-weight:400'">
</div>
<!-- Writing a -->
<div :style="[{float: id === '12' ? 'left:'right}]"></div>
<!-- Write two -->
<div :style="float: nameList.length === 20 ? 'height:64px' : 'height:32px' "></div>
<!-- Write three -->
<div :style="{border:( nameId ===item.id ?'2px solid #4C78FF': 'red')}"></div>
One 4 binding data object
<div :style="styleObject"></div>
<script>
data() {
return{
// stay data Define the initial style values in
styleObject: {
color: 'red',
fontSize: '14px'
}
}
}
</scrip>
Two : class How to write it
Two 1 object form
<!-- isActive — Decision value is : true/false -->
<div :class="{ 'active': isActive }">{
{name}}</div>
Two 2 Determine whether to bind a active
<div :class="{'active' : isActive == -2}" >{
{name}}</div>
<div :class="{'active' : isActive == item.nameId}" >{
{item.name}}</div>
Two 3 Bind and judge multiple
Two 3.1 The first one is Separated by commas
<div :class="{ 'active': isActive, 'user': isUser }"></div>
Two 3.2 Put it in data Inside
<div :class="classObject">{
{name}}</div>
<script>
data() {
return {
classObject:{
active: true,
user:false
}
}
}
</script>
Two 3.3 Use computed Compute properties
<div :class="classObject">{
{name}}</div>
<script>
data() {
return {
isActive: true,
isUser: false
}
},
computed: {
classObject: function () {
return {
active: this.isActive,
user:this.isUser
}
}
}
</script>
Two 4 Array form
Two 4.1 Simple arrays
<div :class="[isActive,isUser]">{
{name}}</div>
<script>
data() {
return{
isActive:'active',
isUser:'user'
}
}
</script>
Two 4.2 Array and ternary operator are combined to judge and select the required class
Be careful : After the ternary operator The colon ,
On both sides class You need to put single quotes , Otherwise, it will not render correctly
:class = " [ isActive ? 'active' : ' '] "
perhaps
:class = "[ isActive1 ? 'active' : ' ' ] "
perhaps
:class= " [ isActiveindex ? 'active' : ' ' ] "
perhaps
:class=" [ isActive == index ? 'active' : 'otherActiveClass' ] "
Two 4.3 Array objects combined with dynamic judgment
// This is the front. active You can put no single quotation marks inside the object , This one at the back sort Use single quotes 、
:class="[
{ active: isActive }, 'sort'
]"
perhaps
:class="[
{ active: isActive1 }, ‘sort’
]"
perhaps
:class="[
{ active: isActiveindex }, ‘sort’
]"
边栏推荐
猜你喜欢

NLP paper lead reading | what about the degradation of text generation model? Simctg tells you the answer

Lambda 表达式原理分析学习(2022.06.23)

哈夫曼樹(一)基本概念與C語言實現
![Jerry's touch key recognition process [chapter]](/img/3e/bb73c735d0a7c7a26989c65a432dad.png)
Jerry's touch key recognition process [chapter]

The Commission is so high that everyone can participate in the new programmer's partner plan

maya房子建模

神经网络入门(上)

DEX文件解析 - method_ids解析

Evolution of screen display technology

Lumiprobe染料酰肼丨BDP FL 酰肼方案
随机推荐
Implementation principle of PostgreSQL heap table storage engine
Go learning notes
SQL优化
杰理之关于长按开机检测抬起问题【篇】
Evolution of screen display technology
Qt和其它GUI库的对比
NLP skill tree learning route - (I) route overview
HeartBeat基于CRM使用NFS对MySQL高可用
PHP文件上传小结(乱码,移动失败,权限,显示图片)
Heartbeat 与DRBD 配置过程
数据库 OLAP、OLTP是什么?相同和不同?适用场景
Encoding type of Perl conversion file
Big God explains open source buff gain strategy live broadcast
断点续传和下载原理分析
DEX file parsing - Method_ IDS resolution
MySQL master-slave synchronization
Jerry's question about long press boot detection [chapter]
Lumiprobe染料 NHS 酯丨BDP FL NHS 酯研究
NLP paper lead reading | what about the degradation of text generation model? Simctg tells you the answer
Jenkins打包拉取不到最新的jar包