Vue3實現(xiàn)點擊按鈕實現(xiàn)文字變色功能
1.動態(tài)樣式實現(xiàn)
1.1核心代碼解釋:
class="power-station-perspective-item-text":- 為這個
span元素添加了一個 CSS 類,以便對其樣式進行定義。
- 為這個
@click="clickItem(item.id)":- 這是一個 Vue 事件綁定。當用戶點擊這個
span元素時,會觸發(fā)clickItem方法,并將item.id作為參數(shù)傳遞給該方法。這用于記錄用戶點擊了哪個項目。
- 這是一個 Vue 事件綁定。當用戶點擊這個
:style="{ color: isChecked(item.id) ? '#cc7e17' : '' }":- 這是一個 Vue 動態(tài)綁定的內聯(lián)樣式。
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.動態(tài)類名
2.1核心代碼解釋
說明:
:class綁定::class是 Vue 提供的一個特性,用于綁定動態(tài)類名。- 在這里,
:class綁定了一個數(shù)組,其中包含了兩個元素。
數(shù)組語法:
- 數(shù)組的第一個元素是
'power-station-perspective-item-text':- 這意味著每個
span元素都會始終應用這個基礎類,確?;緲邮浇y(tǒng)一。
- 這意味著每個
- 數(shù)組的第二個元素是一個對象:
{ '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元素上;否則,不會應用。
- 數(shù)組的第一個元素是
: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.實現(xiàn)效果

到此這篇關于Vue3實現(xiàn)點擊按鈕實現(xiàn)文字變色功能的文章就介紹到這了,更多相關Vue3點擊按鈕文字變色內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
如何解決element-ui中select下拉框popper超出彈框問題
這篇文章主要介紹了如何解決element-ui中select下拉框popper超出彈框問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
Vue mockjs編寫假數(shù)據(jù)并請求獲取實現(xiàn)流程
這篇文章主要介紹了Vue mockjs編寫假數(shù)據(jù)并請求獲取實現(xiàn)流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-12-12
詳解vuejs2.0 select 動態(tài)綁定下拉框支持多選
這篇文章主要介紹了vuejs2.0 select動態(tài)綁定下拉框 ,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04

