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

前端 CSS 动态设置样式::class、:style 等技巧详解

一、:class 动态绑定类名

v-bind:class(缩写为 :class)可以动态地绑定一个或多个 CSS 类名。

1. 对象语法

通过对象语法,可以根据条件动态切换类名。

<template><div :class="{ greenText: isActive, 'red-text': hasError }">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
</template><script>
export default {data() {return {isActive: true,hasError: false,};},
};
</script><style>
.greenText {color: green;
}
.red-text {color: red;
}
</style>
  • greenText:当 isActive 为 true 时,添加 greenText 类。
  • red-text:当 hasError 为 true 时,添加 red-text 类。

效果图:
在这里插入图片描述

2. 数组语法

通过数组语法,可以同时绑定多个类名。

<template><div :class="[textClass, bgcClass]">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
</template><script>
export default {data() {return {textClass: 'greenText',bgcClass: 'pinkBgc',};},
};
</script><style>
.greenText {color: green;
}
.pinkBgc {width: 300px;height: 200px;background-color: pink;margin: 200px auto;
}
</style>
  • textClass 和 bgcClass 是数据属性,它们的值会同时作为类名绑定到元素上。

效果图:
在这里插入图片描述

3. 结合计算属性

当类名的逻辑较为复杂时,可以使用计算属性来动态生成类名对象。

<template><div :class="computedClass">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
</template><script>
export default {data() {return {isActive: true,hasError: true};},computed: {computedClass() {return {greenText: this.isActive && !this.hasError,'text-red': this.hasError};}}
};
</script><style>
.greenText {color: green;
}
.text-red{color: red;
}
</style>
  • greenText:isActive 为true并且hasError为false的时候生效;
  • text-red:hasError 为true的时候生效;
    效果图:
    在这里插入图片描述

二、:style 动态绑定内联样式

v-bind:style(缩写为 :style)可以动态地绑定内联样式。

1. 对象语法

通过对象语法,可以直接绑定样式对象。

<template><div :style="{ color: activeColor, fontSize: fontSize + 'px' }">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
</template><script>
export default {data() {return {activeColor: 'red',fontSize: 12};},
};
</script>
  • activeColor 和 fontSize 是数据属性,它们的值会作为样式绑定到元素上。
    效果图:
    在这里插入图片描述
2. 数组语法

通过数组语法,可以同时绑定多个样式对象。

<template><div :style="[styles1, styles2]">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
</template><script>
export default {data() {return {styles1: {color: 'red',fontSize: '14px'},styles2: {fontWeight: 'bold',textDecoration: 'underline'}};},
};
</script>
  • styles1 和 styles2 的所有样式都会绑定到元素上。
    效果图:
    在这里插入图片描述
3. 使用三元表达式

可以在 :style 中使用三元表达式,根据条件动态设置样式值。

<template><div :style="{ color: isActive ? 'green' : 'red' }">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
</template><script>
export default {data() {return {isActive: true};},
};
</script>

效果图:
在这里插入图片描述

4. 使用模板字符串

可以使用模板字符串动态拼接样式值。

<template><div :style="`color: ${isActive ? 'green' : 'red'}; font-size: ${fontSize}px`" class="demo">海绵宝宝不喜欢侬,喜欢章鱼哥。</div>
</template><script>
export default {data() {return {isActive: false,fontSize: 12};},
};
</script>

效果图:
在这里插入图片描述

祝大家新年快乐!

http://www.lryc.cn/news/534776.html

相关文章:

  • qt widget和qml界面集成到一起
  • BUU30 [网鼎杯 2018]Fakebook1
  • 信息科技伦理与道德3-2:智能决策
  • 《代码随想录第二十八天》——回溯算法理论基础、组合问题、组合总和III、电话号码的字母组合
  • PromptSource官方文档翻译
  • USB子系统学习(四)用户态下使用libusb读取鼠标数据
  • Ansible简单介绍及用法
  • 目前推荐的优秀编程学习网站与资源平台,涵盖不同学习方式和受众需求
  • 软件工程-软件需求规格说明(SRS)
  • 运维_Mac环境单体服务Docker部署实战手册
  • UE5.5 PCGFrameWork--GPU CustomHLSL
  • RabbitMQ 如何设置限流?
  • json格式,curl命令,及轻量化处理工具
  • Postman面试问题
  • 【JVM详解四】执行引擎
  • esp32 udp 客户端 广播
  • nginx日志存储access日志和error保留180天,每晚把前一天的日志文件压缩成tar.gz
  • 【Java】多线程和高并发编程(四):阻塞队列(上)基础概念、ArrayBlockingQueue
  • C#控件开发6—旋转按钮
  • 在亚马逊云科技上云原生部署DeepSeek-R1模型(下)
  • C# COM 组件在.NET 平台上的编程介绍
  • 火热的大模型: AIGC架构解析
  • Android LifecycleOwner 闪退,java 继承、多态特性!
  • PHP 完整表单实例
  • 深度学习学习笔记(32周)
  • Web3 开发者的机遇与挑战:技术趋势与职业发展
  • 探索robots.txt:网站管理者的搜索引擎指南
  • LM Studio本地调用模型的方法
  • 防火墙安全综合实验
  • uniapp 编译生成鸿蒙正式app步骤