当前位置: 首页 > article >正文

动态绑定style/class(三目运算符)

动态绑定style/class(三目运算符)

  • 1. 动态绑定style
    • 1.1 绑定单个style
    • 1.2 绑定多个style
  • 2. 动态绑定class
    • 2.2 绑定单个class
    • 2.2 绑定多个class

1. 动态绑定style

一般情况下使用style是这样的

<a-icon type="desktop" style="fontsize: 25px; color: red" />

1.1 绑定单个style

当我们需要根据不同的状态来动态改变style时,这里以color为例子,我们可以采用三目运算符来实现( : ? )

例如现在有三种状态,当状态为ok时让color为red,当状态为ok2时让color为blue,当状态为ok3时让color为green。

:style="{fontsize: '25px',color: item.statu == 'ok' ? 'red' : item.statu == 'ok2' ? 'blue' : 'green'}"

1.2 绑定多个style

:style="[{fontsize: '25px',color: item.statu == 'ok' ? 'red' : item.statu == 'ok2' ? 'blue' : 'green'},{height: !$route.meta.footer == true ? 'auto' : '100%'}]"

2. 动态绑定class

2.2 绑定单个class

一般情况下class是这样的

<a-icon type="desktop" class="deskTop1" />

同样的,我们也可以用三目运算符来动态选择我们所需要的不同的class。

<a-icon type="desktop" :class="[ item.statu == 'ok' ? 'deskTop1' : 'deskTop2' ]" />

2.2 绑定多个class

//需要判断多个是否都使用
<div class="activeOne" :class="{ activeTwo: isActive, activeThree: hasError }"></div>

data() {
    return {
      isActive: true,
      hasError: false
    };
}

//不需要判断多个是否都使用
<div :class="[activeTwo, activeThree]"></div>

http://www.kler.cn/a/14252.html

相关文章:

  • ajax的介绍及使用
  • 区块链技术与应用(赵其刚等)课后题答案
  • Java的多线程和单例设计模式
  • 【k8s】ruoyi微服务迁移到k8s (待写)
  • #Chrome扩展程序开发教程--01:基本概念介绍
  • 独立看门狗(IWDT)
  • Android进阶宝典—Hook
  • 基于Java+Spring+vue+element社区疫情服务平台设计和实现
  • Linux操作系统命令大全
  • 【超算/先进计算学习】日报5
  • 数据结构与算法八 优先队列
  • 2023上半年软考准考证打印时间出来啦~
  • LeetCode:206. 反转链表
  • 【Idea】人工智能编程他来了,Idea集成一款和ChatGPT一样智能的编码辅助神器
  • 使用Docker部署wikitten个人知识库
  • item_search_best-天猫畅销榜API接入详情说明
  • 【学习笔记】unity脚本学习(六)【GUI发展历程、IMGUI控件、Layout自动布局】
  • PCB阻焊桥存在的DFM(可制造性)问题,华秋一文告诉你
  • 有趣的 Kotlin 0x14:Base64编码
  • 【C++】文件IO流及stringstream流的使用