前端 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>效果图:

到此这篇关于前端 CSS 动态设置样式::class、:style 等技巧(推荐)的文章就介绍到这了,更多相关css动态设置样式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
相关文章
本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋友跟随小编一起看看吧2025-06-20- 本文给大家讲解CSS 的三种核心布局机制——普通流(Normal Flow)、浮动(Float)和定位(Positioning)对于创建灵活、响应式的网页至关重要,本文将深入探讨这三种机制的工作原2025-06-19
文章介绍如何用CSS实现角标效果,通过.active类结合::after和::before伪元素,利用定位、边框和旋转创建红色边框与白色三角形的提示标志,适用于按钮或卡片元素的视觉增强设计2025-06-19CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)
CSS Anchor Positioning是一项仍在草案中的新特性,由 Chrome 125 开始提供原生支持需启用实验 flag,它允许你在 CSS 中通过锚点(anchor)来相对于任意 DOM 元素定位,本文2025-06-17CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比
CSS 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关系,以下是 static、relative、absolute、fixed、sticky 的详细对比和应用2025-06-17CSS place-items: center解析与用法详解
place-items: center; 是一个强大的 CSS 简写属性,用于同时控制 网格(Grid) 和 弹性盒(Flexbox) 布局中的对齐方式,本文给大家介绍CSS place-items: center; 详解与用法2025-06-17
在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧2025-06-17- CSS单位区别与使用场景总结:px绝对、vw/vh响应式,%继承父尺寸,em/rem文字缩放,vmin/vmax适应宽高变化,固定布局用px或%,响应式布局用vw/vh/rem,文字用em或rem,本文给大家2025-06-16
- 这篇文章主要介绍了CSS 样式表的四种应用方式及css注释的应用小结,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧2025-05-21
- 在CSS布局中,padding属性是控制元素内容与其边框之间距离的关键工具,本文介绍CSS基础中padding,通过本文的介绍,我们深入了解了padding的基本概念、简写方法以及它对元2025-05-16





最新评论