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

 更新时间:2024年07月01日 09:36:45   作者:FOREVER-Q  
这篇文章主要介绍了使用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 插值语法,用于显示每个项目的标题文本。
     <span
            class="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"
      >
        <span
            class="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"
      >
        <span
            class="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.实现效果

到此这篇关于Vue3实现点击按钮实现文字变色功能的文章就介绍到这了,更多相关Vue3点击按钮文字变色内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue前端左侧菜单右侧内容的网站界面制作过程

    Vue前端左侧菜单右侧内容的网站界面制作过程

    这篇文章主要介绍了使用Vue和ElementUI制作一个带有左侧菜单和右侧内容区的网站页面的过程,文中通过CSS样式和深度作用符,实现了页面的美化和功能的完善,需要的朋友可以参考下
    2025-02-02
  • vue使用codemirror的两种用法

    vue使用codemirror的两种用法

    这篇文章主要介绍了在vue里使用codemirror的两种用法,每种方法通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-08-08
  • 用了babel还需要polyfill吗原理解析

    用了babel还需要polyfill吗原理解析

    这篇文章主要为大家介绍了用了babel是否还需要polyfill的原理解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • 如何解决element-ui中select下拉框popper超出弹框问题

    如何解决element-ui中select下拉框popper超出弹框问题

    这篇文章主要介绍了如何解决element-ui中select下拉框popper超出弹框问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • Vue mockjs编写假数据并请求获取实现流程

    Vue mockjs编写假数据并请求获取实现流程

    这篇文章主要介绍了Vue mockjs编写假数据并请求获取实现流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-12-12
  • 关于vue中watch检测到不到对象属性的变化的解决方法

    关于vue中watch检测到不到对象属性的变化的解决方法

    本篇文章主要介绍了关于vue中watch检测到不到对象属性的变化的解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • vue如何优雅的使用全局WebSocket

    vue如何优雅的使用全局WebSocket

    这篇文章主要介绍了vue如何优雅的使用全局WebSocket问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • Vue下拉选择框Select组件使用详解(一)

    Vue下拉选择框Select组件使用详解(一)

    这篇文章主要为大家详细介绍了Vue下拉选择框Select组件的使用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • 详解vuejs2.0 select 动态绑定下拉框支持多选

    详解vuejs2.0 select 动态绑定下拉框支持多选

    这篇文章主要介绍了vuejs2.0 select动态绑定下拉框 ,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • Vue参数的增删改实例详解

    Vue参数的增删改实例详解

    这篇文章主要为大家详细介绍了Vue参数的增删改实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03

最新评论