当前位置: 代码网 > it编程>编程语言>Javascript > Vue3实现点击按钮实现文字变色功能

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

2024年07月03日 Javascript 我要评论
1.动态样式实现1.1核心代码解释:class="power-station-perspective-item-text":为这个span元素添加了一个 css 类,以便对其样式进

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点击按钮文字变色内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com