Vue的hover/click事件如何動(dòng)態(tài)改變顏色和背景色
hover/click事件動(dòng)態(tài)改變顏色和背景色
hover和click事件共存,動(dòng)態(tài)改變按鈕背景色和文字顏色,不需要用到增刪class類,增刪class類是樣式寫(xiě)死,體驗(yàn)不好!
1.父組件內(nèi)容
<!-- :changeColor為傳入的顏色數(shù)據(jù) --> <head-bar-item path="/home" :changeColor="{color: '#dc5d48', bgColor: '#373833'}"> <template v-slot:item-text>首頁(yè)</template> </head-bar-item> <head-bar-item path="/category"> <template v-slot:item-text>分類</template> </head-bar-item>
2.子組件內(nèi)容(配合路由跳轉(zhuǎn))
<template> <span class="tab-bar-item" :style="changeStyle" @click="itemClick" @mouseover="itemHover" @mouseout="removeHover" > <slot name="item-text"></slot> </span> </template> <script> export default { name: "HeadBarItem", props: { path: String, changeColor: { type: Object, default() { return { color: "#dc5d48", bgColor: "#373833" }; }, }, }, data() { return { isHover: false, }; }, computed: { isActive() { return this.$route.path.includes(this.path); }, //計(jì)算屬性改變顏色核心 //過(guò)程:如果按鈕被點(diǎn)擊了,則為用戶傳入的顏色,否則在判斷鼠標(biāo)是否移入改變了isHover,移入則變色,否則為默認(rèn)值 changeStyle() { return { color: this.isActive ? this.changeColor.color : this.isHover ? this.changeColor.color : "#f8f8f2", backgroundColor: this.isActive ? this.changeColor.bgColor : this.isHover ? this.changeColor.bgColor : "#545453", }; }, }, methods: { itemClick() { //點(diǎn)擊實(shí)現(xiàn)路由跳轉(zhuǎn) this.$router.replace(this.path); }, itemHover() { this.isHover = true; }, removeHover() { this.isHover = false; }, }, }; </script>
vue頁(yè)面背景顏色修改
由于vue的單頁(yè)面應(yīng)用導(dǎo)致我們的項(xiàng)目只有一個(gè)index.html文件,然而我們?nèi)粝敫淖冺?yè)面的背景色美酒必須動(dòng)態(tài)改變body的背景色才是最直觀最有效的解決方案。
廢話不多說(shuō)直接上代碼,親測(cè)百分之百有效:
<template> <div> </div> </template> <script> export default { data() { return {}; }, mounted(){}, methods: {}, //實(shí)例創(chuàng)建之前鉤子函數(shù)--給body添加行內(nèi)樣式 beforeCreate () { this.$nextTick(()=>{ document.body.setAttribute('style', 'background:#EAECF1') }) }, //實(shí)例銷毀之前鉤子--移除body 標(biāo)簽的屬性style beforeDestroy () { document.body.removeAttribute('style') } }; </script> <style lang="scss" scoped></style>
下面說(shuō)下為什么要在beforeCreate鉤子內(nèi)部用this.$nextTick鉤子包裹,this.$nextTick的作用是dom完全加載完成,所以我們改變body背景顏色是在操作dom
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Ant Design Vue Pro動(dòng)態(tài)路由加載,服務(wù)器重啟首頁(yè)白屏問(wèn)題
這篇文章主要介紹了Ant Design Vue Pro動(dòng)態(tài)路由加載,服務(wù)器重啟首頁(yè)白屏問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue之input輸入框防抖debounce函數(shù)的使用方式
這篇文章主要介紹了vue之input輸入框防抖debounce函數(shù)的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Vue3中的常見(jiàn)組件通信之v-model使用詳解
這篇文章主要介紹了Vue3中的常見(jiàn)組件通信之v-model使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04Ant Design的可編輯Tree的實(shí)現(xiàn)操作
這篇文章主要介紹了Ant Design的可編輯Tree的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10vue+vuex+json-seiver實(shí)現(xiàn)數(shù)據(jù)展示+分頁(yè)功能
這篇文章主要介紹了vue+vuex+json-seiver實(shí)現(xiàn)數(shù)據(jù)展示+分頁(yè)功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04vite+vue3項(xiàng)目初始化搭建的實(shí)現(xiàn)步驟
本文主要介紹了vite+vue3項(xiàng)目初始化搭建的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07Vue實(shí)現(xiàn)路由跳轉(zhuǎn)至外界頁(yè)面
這篇文章主要介紹了Vue實(shí)現(xiàn)路由跳轉(zhuǎn)至外界頁(yè)面方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12