欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3實(shí)現(xiàn)點(diǎn)擊按鈕實(shí)現(xiàn)文字變色功能

 更新時(shí)間:2024年07月01日 09:36:45   作者:FOREVER-Q  
這篇文章主要介紹了使用Vue3實(shí)現(xiàn)點(diǎn)擊按鈕實(shí)現(xiàn)文字變色功能,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下

1.動(dòng)態(tài)樣式實(shí)現(xiàn)

1.1核心代碼解釋:

  • class="power-station-perspective-item-text"

    • 為這個(gè) span 元素添加了一個(gè) CSS 類,以便對(duì)其樣式進(jìn)行定義。
  • @click="clickItem(item.id)"

    • 這是一個(gè) Vue 事件綁定。當(dāng)用戶點(diǎn)擊這個(gè) span 元素時(shí),會(huì)觸發(fā) clickItem 方法,并將 item.id 作為參數(shù)傳遞給該方法。這用于記錄用戶點(diǎn)擊了哪個(gè)項(xiàng)目。
  • :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)一。
    • 數(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)用。
 :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前端左側(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
  • vue使用codemirror的兩種用法

    vue使用codemirror的兩種用法

    這篇文章主要介紹了在vue里使用codemirror的兩種用法,每種方法通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 用了babel還需要polyfill嗎原理解析

    用了babel還需要polyfill嗎原理解析

    這篇文章主要為大家介紹了用了babel是否還需要polyfill的原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • 如何解決element-ui中select下拉框popper超出彈框問題

    如何解決element-ui中select下拉框popper超出彈框問題

    這篇文章主要介紹了如何解決element-ui中select下拉框popper超出彈框問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Vue mockjs編寫假數(shù)據(jù)并請(qǐng)求獲取實(shí)現(xiàn)流程

    Vue 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ì)象屬性的變化的解決方法

    本篇文章主要介紹了關(guān)于vue中watch檢測(cè)到不到對(duì)象屬性的變化的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • vue如何優(yōu)雅的使用全局WebSocket

    vue如何優(yōu)雅的使用全局WebSocket

    這篇文章主要介紹了vue如何優(yōu)雅的使用全局WebSocket問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Vue下拉選擇框Select組件使用詳解(一)

    Vue下拉選擇框Select組件使用詳解(一)

    這篇文章主要為大家詳細(xì)介紹了Vue下拉選擇框Select組件的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 詳解vuejs2.0 select 動(dòng)態(tài)綁定下拉框支持多選

    詳解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
  • Vue參數(shù)的增刪改實(shí)例詳解

    Vue參數(shù)的增刪改實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了Vue參數(shù)的增刪改實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03

最新評(píng)論