Vue3 v-bind绑定css中的var变量实现动态样式

在日常的开发中,我们常常遇到这样的需求:点击一个button改变页面中某个元素的样式,在这样的场景中,我们可以使用v-bind绑定css中的var变量,来动态的切换元素的样式

一个小栗子,在setup语法糖环境下,点击一个button动态切换另一个元素的背景色

javascript 复制代码
<template>
  <div class="box">
    <div class="intro">
      <div class="btxt" :style="{'--text-color':textColor}">使用v-bind绑定语法糖中的颜色常量的值给style中的变量</div>
    </div>
    <div class="intro">
      <div class="btn" @click="changeColor">点击button改变textColor的值,动态更新颜色的值</div>
    </div>
  </div>
</template>
<script setup>
import { ref } from 'vue'
const textColor = ref("blue");
const changeColor = () => {
  if (textColor.value === "blue") {
    textColor.value = "pink";
  } else {
    textColor.value = "blue"
  } 
  
};
</script>
<style scoped lang="less">
.box{
  display: flex;
  flex-direction: column;

  .intro{
    background-color: antiquewhite;
    margin: 10px;
    padding: 15px;

    .btn{
      display: inline-block;
      padding: 18px 35px 18px 35px;
    }
  }
}
.btxt{
  padding: 18px 35px 18px 35px;
  color: var(--text-color);
}
</style>

一个小栗子,在非setup语法糖的环境下使用v-bind绑定css中的var变量,实现当鼠标悬停在一个button上时配置button的伪类hover中的背景色,点击button后通过切换var变量对应的颜色值,动态切换button的背景色,代码如下:

javascript 复制代码
<template>
  <div class="box">
    <div class="btn" :style="{'--color-back':backColor}" @click="changeEvent">
      使用v-bind绑定非setup语法糖环境下的style值
    </div>
  </div>
</template>
<script>
export default{
  name:"ChangeButton",
  data(){
    return {
      backColor: "cadetblue"
    }
  },
  methods:{
    changeEvent(){
      let color = this.$data.backColor
      if (color === "cadetblue") {
        this.$data.backColor = "orange"
      } else {
        this.$data.backColor = "cadetblue"
      }
    }
  }
}
</script>
<style scoped lang="less">
.box{
  .btn{
    background-color: var(--color-back);
    display: inline-block;
    padding: 18px 35px 18px 35px;
    border-radius: 5px;
  }
}
.btn:hover{
  background-color: lightgrey;
}
.btn:active{
  animation: changeck 0.3s ease;
}
@keyframes changeck {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(0.95);
  }
  100% {
    transform: scale(1);
  }
}
</style>
javascript 复制代码
<template>
  <div class="box">
    <div class="intro">
      <change-button></change-button>
    </div>
  </div>
</template>
<script setup>
import ChangeButton from './components/index.vue'
</script>
<style scoped lang="less">
.box{
  display: flex;
  flex-direction: column;
}
</style>
相关推荐
水月wwww6 分钟前
vue学习之组件与标签
前端·javascript·vue.js·学习·vue
合作小小程序员小小店20 分钟前
web网页开发,在线%商城,电商,商品购买%系统demo,基于vscode,apache,html,css,jquery,php,mysql数据库
开发语言·前端·数据库·mysql·html·php·电商
顾安r21 分钟前
11.8 脚本网页 塔防游戏
服务器·前端·javascript·游戏·html
草莓熊Lotso28 分钟前
C++ 方向 Web 自动化测试实战:以博客系统为例,从用例到报告全流程解析
前端·网络·c++·人工智能·后端·python·功能测试
fruge36 分钟前
Canvas/SVG 冷门用法:实现动态背景与简易数据可视化
前端·信息可视化
一 乐40 分钟前
旅游|内蒙古景点旅游|基于Springboot+Vue的内蒙古景点旅游管理系统设计与实现(源码+数据库+文档)
开发语言·前端·数据库·vue.js·spring boot·后端·旅游
驯狼小羊羔1 小时前
学习随笔-require和import
前端·学习
excel1 小时前
🚀 从 GPT-5 流式输出看现代前端的流式请求机制(Koa 实现版)
前端
凸头1 小时前
Spring Boot接收前端参数的注解总结
前端·spring boot·后端
爱吃甜品的糯米团子1 小时前
JavaScript 正则表达式:选择、分组与引用深度解析
前端·javascript·正则表达式