vuejs 切換導(dǎo)航條高亮(路由菜單高亮)的方法示例
我的GitHub前端經(jīng)驗(yàn)總結(jié),喜歡的話請(qǐng)點(diǎn)star:Thanks.: https://github.com/liangfengbo/frontend-develop
vuejs導(dǎo)航條高亮我的做法:
- 用一個(gè)數(shù)組存導(dǎo)航條,用v-for循環(huán)它,這樣可以減少代碼,二可以使用它的下標(biāo)來(lái)判斷高亮,三還可以獲取后端的導(dǎo)航信息來(lái)遍歷
- 重點(diǎn)是在:routerLink(index, path)函數(shù),傳入當(dāng)前點(diǎn)擊的下標(biāo),自定義一個(gè)下標(biāo),判斷是否相等就用三元符號(hào)判斷多給一個(gè)高亮樣式
- 如何解決刷新就不高亮或第一個(gè)高亮了,很簡(jiǎn)單,監(jiān)聽(tīng)一下當(dāng)前路由在判斷就好了
具體代碼都在下面了
效果圖:
html代碼
<div class="nav-box"> <!-- 導(dǎo)航列表 --> <li class="nav-item" v-for="(item, index) in nav" @click="routerLink(index, item.path)" :key="index"> <!-- 判斷高亮表 --> <p :class=" navIndex === index ? 'item-cn item-cn-active' : 'item-cn'"> {{ item.title }} </p> <!-- 判斷高亮表 --> <p :class="navIndex === index ? 'item-en item-en-active' : 'item-en'"> {{ item.en }} </p> </li> </div>
data數(shù)據(jù)
// 導(dǎo)航條 data() { return { nav: [ {title: '首頁(yè)', en: 'Code', path: '/'}, {title: '開(kāi)源', en: 'Source', path: '/source'}, {title: '關(guān)于', en: 'About', path: '/about'}, ], navIndex: 0, } },
methods方法:
/** * 路由跳轉(zhuǎn) * @param index * @param link */ routerLink(index, path) { // 點(diǎn)擊哪個(gè)路由就賦值給自定義的下下標(biāo) this.navIndex = index; // 路由跳轉(zhuǎn) this.$router.push(path) }, /** * 檢索當(dāng)前路徑 * @param path */ checkRouterLocal(path) { // 查找當(dāng)前路由下標(biāo)高亮 this.navIndex = this.nav.findIndex(item => item.path === path); }
監(jiān)聽(tīng)路由變化獲取當(dāng)前路由
watch: { "$route"() { // 獲取當(dāng)前路徑 let path = this.$route.path; // 檢索當(dāng)前路徑 this.checkRouterLocal(path); } },
css樣式
.nav-box { display: flex; } .nav-item { text-align: center; padding: 0 32px; position: relative; display: inline-block; font-size: 14px; line-height: 25px; } /*導(dǎo)航普通狀態(tài)*/ .item-cn { color: #1c2438; font-weight: 800; } /*導(dǎo)航普通狀態(tài)*/ .item-en { color: #666; font-size: 12px; } /*導(dǎo)航高亮*/ .item-cn-active { color: #2d8cf0; } /*導(dǎo)航高亮*/ .item-en-active { color: #5cadff; } .nav-item:hover .item-cn { color: #2d8cf0; } .nav-item:hover .item-en { color: #5cadff; } .nav-item:after { position: absolute; right: 0; top: 20px; content: ''; width: 1px; height: 16px; background-color: #f8f8f8; } .nav-item:after:last-child { width: 0; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 利用Js+Css實(shí)現(xiàn)折紙動(dòng)態(tài)導(dǎo)航效果實(shí)例源碼
- JS+CSS實(shí)現(xiàn)鼠標(biāo)滑過(guò)時(shí)動(dòng)態(tài)翻滾的導(dǎo)航條效果
- javascript實(shí)現(xiàn)的鼠標(biāo)懸停時(shí)動(dòng)態(tài)翻滾的導(dǎo)航條
- JS實(shí)現(xiàn)移動(dòng)端可折疊導(dǎo)航菜單(現(xiàn)代都市風(fēng))
- JS無(wú)限級(jí)導(dǎo)航菜單實(shí)現(xiàn)方法
- js實(shí)現(xiàn)水平滾動(dòng)菜單導(dǎo)航
- 原生JS實(shí)現(xiàn)導(dǎo)航下拉菜單效果
- 如何使用wheelnav.js構(gòu)建酷炫的動(dòng)態(tài)導(dǎo)航菜單
相關(guān)文章
vue單頁(yè)面打包文件大?首次加載慢?nginx帶你飛,從7.5M到1.3M蛻變過(guò)程(推薦)
這篇文章主要介紹了vue單頁(yè)面打包文件大?首次加載慢?nginx帶你飛,從7.5M到1.3M蛻變過(guò)程,需要的朋友可以參考下2018-01-01一文搞懂Vue3中的異步組件defineAsyncComponentAPI的用法
這篇文章主要介紹了一文搞懂Vue3中的異步組件,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07vue $nextTick實(shí)現(xiàn)原理深入詳解
這篇文章主要介紹了vue $nextTick實(shí)現(xiàn)原理深入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10vue3如何實(shí)現(xiàn)表格內(nèi)容無(wú)縫滾動(dòng)(又寫(xiě)了一堆冗余代碼)
這篇文章主要給大家介紹了關(guān)于vue3如何實(shí)現(xiàn)表格內(nèi)容無(wú)縫滾動(dòng)的相關(guān)資料,在Vue3項(xiàng)目中難免會(huì)遇到讓列表滾動(dòng)的需求,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04解決運(yùn)行vue項(xiàng)目?jī)?nèi)存溢出問(wèn)題
這篇文章主要介紹了解決運(yùn)行vue項(xiàng)目?jī)?nèi)存溢出問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04關(guān)于vue-admin-template模板連接后端改造登錄功能
這篇文章主要介紹了關(guān)于vue-admin-template模板連接后端改造登錄功能,登陸方法根據(jù)賬號(hào)密碼查出用戶信息,根據(jù)用戶id與name生成token并返回,userinfo則是對(duì)token進(jìn)行獲取,在查出對(duì)應(yīng)值進(jìn)行返回,感興趣的朋友一起看看吧2022-05-05