Vue3實(shí)現(xiàn)點(diǎn)擊按鈕實(shí)現(xiàn)文字變色功能
1.動(dòng)態(tài)樣式實(shí)現(xiàn)
1.1核心代碼解釋:
class="power-station-perspective-item-text":- 為這個(gè)
span元素添加了一個(gè) CSS 類,以便對(duì)其樣式進(jìn)行定義。
- 為這個(gè)
@click="clickItem(item.id)":- 這是一個(gè) Vue 事件綁定。當(dāng)用戶點(diǎn)擊這個(gè)
span元素時(shí),會(huì)觸發(fā)clickItem方法,并將item.id作為參數(shù)傳遞給該方法。這用于記錄用戶點(diǎn)擊了哪個(gè)項(xiàng)目。
- 這是一個(gè) Vue 事件綁定。當(dāng)用戶點(diǎn)擊這個(gè)
:style="{ color: isChecked(item.id) ? '#cc7e17' : '' }":- 這是一個(gè) Vue 動(dòng)態(tài)綁定的內(nèi)聯(lián)樣式。
isChecked(item.id)會(huì)檢查當(dāng)前項(xiàng)目是否被選中(即checkedItem.value是否等于item.id)。- 如果
isChecked(item.id)返回true,則color樣式會(huì)被設(shè)置為'#cc7e17'(一種顏色值);否則,color樣式為空字符串,表示不改變顏色。
{{ item.title }}:- 這是一個(gè) Vue 插值語(yǔ)法,用于顯示每個(gè)項(xiàng)目的標(biāo)題文本。
<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("菜單項(xiàng)");
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.動(dòng)態(tài)類名
2.1核心代碼解釋
說明:
:class綁定::class是 Vue 提供的一個(gè)特性,用于綁定動(dòng)態(tài)類名。- 在這里,
:class綁定了一個(gè)數(shù)組,其中包含了兩個(gè)元素。
數(shù)組語(yǔ)法:
- 數(shù)組的第一個(gè)元素是
'power-station-perspective-item-text':- 這意味著每個(gè)
span元素都會(huì)始終應(yīng)用這個(gè)基礎(chǔ)類,確保基本樣式統(tǒng)一。
- 這意味著每個(gè)
- 數(shù)組的第二個(gè)元素是一個(gè)對(duì)象:
{ 'active-power-station-perspective-item-text': isChecked(item.id) }- 這個(gè)對(duì)象的鍵是
'active-power-station-perspective-item-text',值是一個(gè)布爾表達(dá)式isChecked(item.id)。 - 如果
isChecked(item.id)返回true,則active-power-station-perspective-item-text類會(huì)被應(yīng)用到span元素上;否則,不會(huì)應(yīng)用。
- 數(shù)組的第一個(gè)元素是
: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("菜單項(xiàng)");
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.實(shí)現(xiàn)效果

到此這篇關(guān)于Vue3實(shí)現(xiàn)點(diǎn)擊按鈕實(shí)現(xiàn)文字變色功能的文章就介紹到這了,更多相關(guān)Vue3點(diǎn)擊按鈕文字變色內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Element中table組件(el-table)右側(cè)滾動(dòng)條空白占位處理
當(dāng)我設(shè)置了max-height,就會(huì)在表格右側(cè)出現(xiàn)一列空白的占位,本文主要介紹了Element中table組件(el-table)右側(cè)滾動(dòng)條空白占位處理,感興趣的可以了解一下2023-09-09
解決Vue項(xiàng)目中Emitted value instead of an 
這篇文章主要介紹了解決Vue項(xiàng)目中Emitted value instead of an instance of Error問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
vue3中使用props和emits并指定其類型與默認(rèn)值
props是Vue3中的一個(gè)重要概念,它允許我們將數(shù)據(jù)從父組件傳遞到子組件,下面這篇文章主要給大家介紹了關(guān)于vue3中使用props和emits并指定其類型與默認(rèn)值的相關(guān)資料,需要的朋友可以參考下2023-04-04
vue中路由跳轉(zhuǎn)不計(jì)入history的操作
這篇文章主要介紹了vue中路由跳轉(zhuǎn)不計(jì)入history的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue項(xiàng)目中頁(yè)面跳轉(zhuǎn)傳參的方法總結(jié)
在Vue項(xiàng)目中,你可以使用路由(vue-router)來實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)并傳遞參數(shù),這篇文章主要為大家整理了一些常用的方法,感興趣的小伙伴可以學(xué)習(xí)一下2023-11-11
基于Vue2.0+ElementUI實(shí)現(xiàn)表格翻頁(yè)功能
Element UI 是一套采用 Vue 2.0 作為基礎(chǔ)框架實(shí)現(xiàn)的組件庫(kù),它面向企業(yè)級(jí)的后臺(tái)應(yīng)用,能夠幫助你快速地搭建網(wǎng)站,極大地減少研發(fā)的人力與時(shí)間成本。這篇文章主要介紹了Vue2.0+ElementUI實(shí)現(xiàn)表格翻頁(yè)功能,需要的朋友可以參考下2017-10-10
vue+iview tabs context-menu 彈出框修改樣式的方法
今天遇到一個(gè)需求說頁(yè)面頂部的菜單右鍵彈出框離得有點(diǎn)遠(yuǎn),需要我們做調(diào)整,下面小編給大家分享下vue+iview tabs context-menu 彈出框修改樣式的方法,感興趣的朋友跟隨小編一起看看吧2024-06-06
vue-music 使用better-scroll遇到輪播圖不能自動(dòng)輪播問題
根據(jù)vue-music視頻中slider組建的使用,當(dāng)安裝新版本的better-scroll,輪播組件,不能正常輪播。如何解決這個(gè)問題呢,下面小編給大家?guī)砹藇ue-music 使用better-scroll遇到輪播圖不能自動(dòng)輪播問題,感興趣的朋友一起看看吧2018-12-12

