Vue3 动态修改CSS样式(包含style + class)- 附示例

在日常的开发过程中,CSS样式根据某个变量的状态或者是变量值的改变而展示不同的属性值是很常见的,故在此记录一些常用的使用场景。

目录

效果

 一、介绍

1、官方文档:Class 与 Style 绑定 | Vue.js

2、官方示例

二、示例

1、class

        1)动态class - 真假值

        2)class - 绑定ref

        3)class - 绑定对象

        4)class - 计算属性

        5)class - 数组形式

        6)动态class - 三元表达式

        7)(动态class - 三元表达式) + 固定class

2、style        

        1)style - 绑定变量

        2)style- 绑定对象

        3)style - 数组形式

        4)style - 样式多值​

        5)动态style - 三元表达式

        6)动态style - 数组对象 + 三元表达式

        7)动态style - 模板字符串(写法一)

        8)动态style - 模板字符串 (写法二)

          9)动态style - 模板字符(多属性)

 欢迎扫描下方二维码关注VX公众号


效果

 一、介绍

1、官方文档:Class 与 Style 绑定 | Vue.js

Class 与 Style 绑定 | Vue.jsVue.js - 渐进式的 JavaScript 框架icon-default.png?t=N7T8https://cn.vuejs.org/guide/essentials/class-and-style.html

2、官方示例

二、示例

注:以下示例的样式切换都是基于<el-switch v-model="valueVM" @change="switchChange" />,具体代码在1、的3)部分展示

1、class

        1)动态class - 真假值
<template>
  <div class="content">
    <div :class="{ 'is-true': !valueVM }">动态class - 真假值</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const valueVM = ref(true);
</script>

<style scoped>
  .is-true {
    color: #13CE66;
  }
</style>
        2)class - 绑定ref
<template>
  <div class="content">
    <div :class="{ 'color-bind': valueVM }">class - 绑定ref</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const valueVM = ref(true);
const colorRef = ref('#F56C6C');
</script>

<style scoped>
  .color-bind {
	color: v-bind('colorRef')
  }
</style>
        3)class - 绑定对象
<template>
  <div class="content">
    <el-switch v-model="valueVM" @change="switchChange" />
    <div :class="classObject">class - 绑定对象</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const valueVM = ref(true);

const classObject = ref({
  'is-true': true,
  'is-false': false
})

const switchChange = (value: boolean) => {
  if (value) {
    classObject.value = {
      'is-true': true,
      'is-false': false
    }
  } else {
    classObject.value = {
      'is-true': false,
      'is-false': true
    }
  }
}
</script>

<style scoped>
  .is-true {
    color: #13CE66;
  }
  .is-false {
    color: #E6A23C;
  }
</style>

注:这里用了Element-plus的Switch开关组件,CSS样式根据开关的状态而变化

Switch 开关 | Element Plus

        4)class - 计算属性
<template>
  <div class="content">
    <div :class="classComputed">class - 计算属性</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const isTrue = ref(true);
const isFalse = ref(null);

const classComputed = computed(() => ({
  'is-normal': isTrue.value && !isFalse.value,
  'is-error': isFalse.value
}))

</script>

<style scoped>
  .is-normal {
    color: #409EFF;
  }  
  .is-error {
    color: #F56C6C;
  }
</style>
        5)class - 数组形式
<template>
  <div class="content">
    <div :class="[activeClass, errorClass]">class - 数组形式</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const activeClass = ref('active');
const errorClass = ref('is-error');

</script>

<style scoped>
  .active {
	font-size: 1.2rem;
  }
  .is-error {
    color: #F56C6C;
  }
</style>
        6)动态class - 三元表达式
<template>
  <div class="content">
    <div :class="[valueVM ? 'is-false' : 'is-true']">动态class - 三元表达式</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const valueVM = ref(true);
</script>

<style scoped>
  .is-false {
    color: #E6A23C;
  }
  .is-true {
    color: #13CE66;
  }
</style>
        7)(动态class - 三元表达式) + 固定class
<template>
  <div class="content">
    <div :class="[valueVM ? 'is-true' : 'is-false', 'active']">(动态class - 三元表达式) + 固定class</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const valueVM = ref(true);
</script>

<style scoped>
  .is-false {
    color: #E6A23C;
  }
  .is-true {
    color: #13CE66;
  }
  .active {
	font-size: 1.2rem;
  }
</style>

2、style        

        1)style - 绑定变量
<template>
  <div class="content">
    <div :style="{ color: colorRef, fontSize: fontSize + 'px' }">style - 绑定变量</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const colorRef = ref('#F56C6C');
const fontSize = ref(20);
</script>
        2)style- 绑定对象
<template>
  <div class="content">
    <div :style="styleObject">style- 绑定对象</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const styleObject = reactive({
  'backgroundColor': '#E6A23C',
  'color': '#DCDFE6'
})
</script>
        3)style - 数组形式
<template>
  <div class="content">
    <div :style="[colorStyle, fontSizeStyle]">style - 数组形式</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const colorStyle = ref({ color: '#13CE66' });
const fontSizeStyle = ref({ fontSize: '20px' });
</script>
        4)style - 样式多值​

        你可以对一个样式属性提供多个 (不同前缀的) 值,举例来说:

<template>
  <div class="content">
    <div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }">style - 样式多值​</div>
  </div>
</template>

        数组仅会渲染浏览器支持的最后一个值。在这个示例中,在支持不需要特别前缀的浏览器中都会渲染为 display: flex。

        5)动态style - 三元表达式
<template>
  <div class="content">
    <div :style="{ color: valueVM ? '#409EFF' : '#DCDFE6', backgroundColor: valueVM ? '#E6A23C' : '#F56C6C' }">动态style - 三元表达式</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const valueVM = ref(true);
</script>
        6)动态style - 数组对象 + 三元表达式
<template>
  <div class="content">
    <div :style="[{ 'backgroundColor': valueVM ? '#409EFF' : '#DCDFE6' }, { 'color': valueVM ? '#E6A23C' : '#F56C6C' }]">动态style - 数组对象 + 三元表达式</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const valueVM = ref(true);
</script>
        7)动态style - 模板字符串(写法一)
<template>
  <div class="content">
   <div :style="`background-color: ${ valueVM ? '#E6A23C' : '#13CE66' }`">动态style - 模板字符(写法一)</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const valueVM = ref(true);
</script>
        8)动态style - 模板字符串 (写法二)
<template>
  <div class="content">
    <div :style="'background-color:' + `${ valueVM ? '#409EFF' : '#F56C6C' }`">动态style - 模板字符(写法二)</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const valueVM = ref(true);
</script>
          9)动态style - 模板字符(多属性)
<template>
  <div class="content">
    <div :style="{ 'background-color': `${ valueVM ? '#13CE66' : '#DCDFE6' }`, 'color' : `${ !valueVM ? '#fff' : '#E6A23C' }` }">动态style - 模板字符(多属性)</div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';

const valueVM = ref(true);
</script>

参考:

Class 与 Style 绑定 | Vue.js

​​​​​​Vue 通过style属性、class属性来动态修改CSS样式_vue动态修改css样式-CSDN博客

 欢迎扫描下方二维码关注VX公众号

要在Vue3中使用Ant Design Vue样式,可以按照以下步骤进行操作: 1. 首先,在你的项目中安装Ant Design Vue组件库。你可以通过npm或者yarn进行安装,具体的安装命令可以在Ant Design Vue的官方文档中找到。 2. 在你的Vue组件中引入Ant Design Vue样式文件。你可以在组件所在的Vue文件中使用import语句引入Ant Design Vue样式文件。 例如,在你的Vue文件中添加以下代码: ```javascript <style lang="less" scoped> @import '~ant-design-vue/dist/antd.less'; </style> ``` 这将会导入Ant Design Vue样式文件,并使它只在当前组件中生效。 3. 接下来,你可以根据需要自定义你的组件样式。你可以在style标签中使用普通的CSS语法来定义和修改组件的样式。 例如,你可以在style标签中添加以下代码来自定义一个按钮组件的样式: ```javascript <style lang="less" scoped> .my-button { border-radius: 10px; } </style> ``` 这将会给按钮组件添加一个圆角为10px的边框样式。 通过以上步骤,你就可以在Vue3中使用Ant Design Vue样式了。记得按照官方文档中的指引导入需要的组件,并在模板中使用它们。如果你需要更多示例和帮助,可以参考官方文档或者Ant Design Vue的开源项目。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue3+ant design vue+ts实战【ant-design-vue组件库引入】](https://blog.csdn.net/XSL_HR/article/details/127396384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [vue3+ant-design-vue按需加载组件](https://blog.csdn.net/qq_42263570/article/details/130143934)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [vue3+element-plus的后台管理系统模板 和 vue3+ant-design-vue的后台管理系统模板](https://blog.csdn.net/qq_61233877/article/details/131008600)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值