vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > Vue3点击按钮文字变色

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

作者:FOREVER-Q

这篇文章主要介绍了使用Vue3实现点击按钮实现文字变色功能,文中通过代码示例给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下

1.动态样式实现

1.1核心代码解释:

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

您可能感兴趣的文章:
阅读全文