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

Vue3实现点击按钮实现文字变色

1.动态样式实现

1.1核心代码解释:

  • class="power-station-perspective-item-text"

    • 为这个 span 元素添加了一个 CSS 类,以便对其样式进行定义。
  • @click="clickItem(item.id)"

    • 这是一个 Vue 事件绑定。当用户点击这个 span 元素时,会触发 clickItem 方法,并将 item.id 作为参数传递给该方法。这用于记录用户点击了哪个项目。
  • :style="{ color: isChecked(item.id) ? '#cc7e17' : '' }"

    • 这是一个 Vue 动态绑定的内联样式。
    • isChecked(item.id) 会检查当前项目是否被选中(即 checkedItem.value 是否等于 item.id)。
    • 如果 isChecked(item.id) 返回 true,则 color 样式会被设置为 '#cc7e17'(一种颜色值);否则,color 样式为空字符串,表示不改变颜色。
  • {{ item.title }}

    • 这是一个 Vue 插值语法,用于显示每个项目的标题文本。
     <spanclass="power-station-perspective-item-text"@click="clickItem(item.id)":style="{ color: isChecked(item.id) ? '#cc7e17' : '' }">{{ item.title }}</span>

1.2源代码

<template><div class="power-station-perspective"><div class="flow-chart-container-item"><div class="power-station-perspective-title flow-chart-container-item-parent">{{ title }}</div><div v-for="item in buttonGroupsArr":key="item.id"class="power-station-perspective-item flow-chart-container-item location"><spanclass="power-station-perspective-item-text"@click="clickItem(item.id)":style="{ color: isChecked(item.id) ? '#cc7e17' : '' }">{{ item.title }}</span></div></div></div>
</template><script setup>
import {ref, onMounted} from "vue";const title = ref("菜单项");
const buttonGroupsArr = ref([{title: "按钮1", id: 0},{title: "按钮2", id: 1},{title: "按钮3", id: 2},{title: "按钮4", id: 3},{title: "按钮5", id: 4},
]);const checkedItem = ref(0);const isChecked = (param) => {return checkedItem.value === param;
};const clickItem = (param) => {checkedItem.value = param;
};onMounted(() => {});
</script><style scoped>
.power-station-perspective{width: 200px;
}
.flow-chart-container-item-parent {width: 100%;background: linear-gradient(90deg, rgba(0, 136, 234, 0.84) 0%,rgba(31, 38, 83, 0.85) 101.82%);
}.flow-chart-container-item {display: grid;text-align: center;padding: 3px 5px 3px 3px;margin-bottom: 3px;align-items: center;
}.power-station-perspective-item {background: rgba(0, 46, 79, 0.5);
}.location {cursor: pointer;
}.power-station-perspective-item-text {margin: 0 auto;cursor: pointer;
}.power-station-perspective-title {margin-bottom: 3px;
}
</style>

2.动态类名

 2.1核心代码解释

说明:

  • :class 绑定:

    • :class 是 Vue 提供的一个特性,用于绑定动态类名。
    • 在这里,:class 绑定了一个数组,其中包含了两个元素。
  • 数组语法:

    • 数组的第一个元素是 'power-station-perspective-item-text'
      • 这意味着每个 span 元素都会始终应用这个基础类,确保基本样式统一。
    • 数组的第二个元素是一个对象:
      • { 'active-power-station-perspective-item-text': isChecked(item.id) }
      • 这个对象的键是 'active-power-station-perspective-item-text',值是一个布尔表达式 isChecked(item.id)
      • 如果 isChecked(item.id) 返回 true,则 active-power-station-perspective-item-text 类会被应用到 span 元素上;否则,不会应用。
 :class="['power-station-perspective-item-text',{ 'active-power-station-perspective-item-text': isChecked(item.id) }]">

 2.2源代码

<template><div class="power-station-perspective"><div class="flow-chart-container-item"><div class="power-station-perspective-title flow-chart-container-item-parent">{{ title }}</div><div v-for="item in buttonGroupsArr":key="item.id"class="power-station-perspective-item flow-chart-container-item location"><spanclass="power-station-perspective-item-text"@click="clickItem(item.id)":class="['power-station-perspective-item-text',{ 'active-power-station-perspective-item-text': isChecked(item.id) }]">{{ item.title }}</span></div></div></div>
</template><script setup>
import {ref, onMounted} from "vue";const title = ref("菜单项");
const buttonGroupsArr = ref([{title: "按钮1", id: 0},{title: "按钮2", id: 1},{title: "按钮3", id: 2},{title: "按钮4", id: 3},{title: "按钮5", id: 4},
]);const checkedItem = ref(0);const isChecked = (param) => {return checkedItem.value === param;
};const clickItem = (param) => {checkedItem.value = param;
};onMounted(() => {});
</script><style scoped>
.power-station-perspective{width: 200px;
}
.flow-chart-container-item-parent {width: 100%;background: linear-gradient(90deg, rgba(0, 136, 234, 0.84) 0%,rgba(31, 38, 83, 0.85) 101.82%);
}.flow-chart-container-item {display: grid;text-align: center;padding: 3px 5px 3px 3px;margin-bottom: 3px;align-items: center;
}.power-station-perspective-item {background: rgba(0, 46, 79, 0.5);
}.location {cursor: pointer;
}.power-station-perspective-item-text {margin: 0 auto;cursor: pointer;
}
.active-power-station-perspective-item-text{color: #cc7e17;
}
.power-station-perspective-title {margin-bottom: 3px;
}
</style>

3.实现效果

 

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

相关文章:

  • 深入理解Vue生命周期钩子函数
  • Linux-gdb
  • Oracle分析表和索引(analyze)
  • MyBatis踩坑记录-多表关联字段相同,字段数据覆盖问题
  • 昇思25天学习打卡营第6天|数据变换 Transforms
  • 在线JSON可视化工具--改进
  • 探讨命令模式及其应用
  • 1、音视频解封装流程---解复用
  • centos7升级gcc到7.3.0
  • 系统运维面试题总结(网络基础类)
  • PO模式登录测试
  • X86 +PC104+支持WinCE5.0,WinCE6.0,DOS,WinXP, QNX等操作系统,工业控制数据采集核心模块板卡定制
  • 视频监控汇聚和融合平台的特点、功能、接入方式、应用场景
  • 实习总结 --- 其他业务
  • 2024年上半年典型网络攻击事件汇总
  • Ozon、美客多补单测评黑科技:打造无懈可击的自养号补单环境
  • ES报错:解决too_many_clauses: maxClauseCount is set to 1024 报错问题
  • 完全指南:在Linux上安装和精通Conda
  • # linux 系统中,使用 “ ll “ 命令报错 “ bash ll command not found “ 解决方法:
  • 吴恩达深度学习笔记:机器学习策略(2)(ML Strategy (2)) 2.3-2.4
  • 【软件测试】快速定位bug,编写测试用例
  • 升级springboot3
  • 视频编解码从H.264到H.266:浅析GB28181安防视频汇聚EasyCVR视频压缩技术
  • vue项目访问 域名/index.html 空页面问题
  • 区块链开发入门:基础概念与实施技术详解
  • Rust破界:前端革新与Vite重构的深度透视(下)
  • Android 解决 “Module was compiled with an incompatible version of Kotlin“ 问题
  • linux nfs的使用
  • eclipse断点调试(用图说话)
  • vue的学习--day2