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

前端 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.kler.cn/a/536667.html

相关文章:

  • 调用腾讯云批量文本翻译API翻译srt字幕
  • 机器学习数学基础:14.矩阵的公式
  • gitlab个别服务无法启动可能原因
  • 五十天精通硬件设计第四天-场效应管知识及选型
  • PostgreSql 函数异常处理
  • 网络面试题(第一部分)
  • 基于WOA鲸鱼优化的TCN时间卷积神经网络时间序列预测算法matlab仿真
  • 【玩转 Postman 接口测试与开发2_019】第15章:利用 Postman 初探 API 性能测试(含实战截图)
  • FFmpeg使用GPU编解码,及在C++代码中实现FFmpeg使用GPU编解码
  • C# LINQ与集合类 数据操作
  • postgresql-15(yum安装教程)
  • 让文物“活”起来,以3D数字化技术传承文物历史文化!
  • [RabbitMQ] 常见面试题汇总 工作流程 消息可靠性 消息顺序性 幂等性 高级特性 延迟队列 仲裁队列 工作模式 消息积压 推拉模式
  • easyxor
  • 赛博算命之 ”梅花易数“ 的 “JAVA“ 实现 ——从玄学到科学的探索
  • element-ui rate 组件源码分享
  • zsh: command not found: pip
  • Android Studio:键值对存储sharedPreferences
  • unity视频在场景中的使用
  • APP广告变现如何优化广告填充率,提升变现收益?
  • Mysql知识梳理(数据库的锁梳理,Mysql优化)
  • 回溯算法中关于剪枝的一些应用
  • PHP PDO 教程
  • 【Elasticsearch】date range聚合
  • 安卓/ios脚本开发按键精灵经验小分享
  • .net一些知识点5