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ǔ)類,確?;緲邮浇y(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)文章
Vue前端左側(cè)菜單右側(cè)內(nèi)容的網(wǎng)站界面制作過程
這篇文章主要介紹了使用Vue和ElementUI制作一個(gè)帶有左側(cè)菜單和右側(cè)內(nèi)容區(qū)的網(wǎng)站頁(yè)面的過程,文中通過CSS樣式和深度作用符,實(shí)現(xiàn)了頁(yè)面的美化和功能的完善,需要的朋友可以參考下2025-02-02如何解決element-ui中select下拉框popper超出彈框問題
這篇文章主要介紹了如何解決element-ui中select下拉框popper超出彈框問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04Vue mockjs編寫假數(shù)據(jù)并請(qǐng)求獲取實(shí)現(xiàn)流程
這篇文章主要介紹了Vue mockjs編寫假數(shù)據(jù)并請(qǐng)求獲取實(shí)現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12關(guān)于vue中watch檢測(cè)到不到對(duì)象屬性的變化的解決方法
本篇文章主要介紹了關(guān)于vue中watch檢測(cè)到不到對(duì)象屬性的變化的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02詳解vuejs2.0 select 動(dòng)態(tài)綁定下拉框支持多選
這篇文章主要介紹了vuejs2.0 select動(dòng)態(tài)綁定下拉框 ,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04