vue中的路由跳轉(zhuǎn)tabBar圖片和文字的高亮效果
前言
在pc端和移動(dòng)端的項(xiàng)目里面會(huì)遇見(jiàn)導(dǎo)航欄或者tabBar的點(diǎn)擊跳轉(zhuǎn),圖片和文字的高亮效果,對(duì)于小程序來(lái)說(shuō)可以直接創(chuàng)建和修改圖片和文字的高亮效果,也可以使用相應(yīng)的組件庫(kù)去自定義一些效果,而在pc端和移動(dòng)端的來(lái)說(shuō)需要對(duì)導(dǎo)航欄或者tabBar進(jìn)行一定的封裝,使其成為全局組件的使用,結(jié)合組件間的數(shù)據(jù)傳遞可以實(shí)現(xiàn)不同頁(yè)面的不同信息的展示,本篇文章介紹路由跳轉(zhuǎn)的時(shí)候,使圖片和文字的高亮效果的方法
定義基本的組件
在demo的初期,我們需要在項(xiàng)目的components文件夾下創(chuàng)建封裝的tabBar組件,創(chuàng)建文件myTabbar.vue,接下來(lái)需要在main.js入口文件引入注冊(cè):
// 引入全局組件tabBar import myTabbar from '@/components/myTabbar' Vue.component('myTabbar', myTabbar)
接下來(lái)需要我們簡(jiǎn)單書(shū)寫(xiě)myTabbar.vue的樣式結(jié)構(gòu):
<template> <div class="tabBar"> <ul> <li v-for="(item, index) in list" :key="index" @click="change(item.path)"> <img :src="item.selected" /> <span>{{ item.title }}</span> </li> </ul> </div> </template> <script> export default { data() { return { list: [ { title: '首頁(yè)', path: '/home', // 需要跳轉(zhuǎn)的地址 active: '../images/home.png', // 這里填寫(xiě)未選擇圖片的路徑 selected: '../images/home-a.png' // 這里填寫(xiě)選擇圖片的路徑,這里以vue圖標(biāo)為例 }, { title: '分類(lèi)', path: '/demo', active: '../images/home.png', // 這里填寫(xiě)未選擇圖片的路徑 selected: '../images/home-a.png' // 這里填寫(xiě)選擇圖片的路徑,這里以vue圖標(biāo)為例 }, { title: '購(gòu)物車(chē)', path: '/cart', active: '../images/home.png', // 這里填寫(xiě)未選擇圖片的路徑 selected: '../images/home-a.png' // 這里填寫(xiě)選擇圖片的路徑,這里以vue圖標(biāo)為例 }, { title: '我們', path: '/my', active: '../images/home.png', // 這里填寫(xiě)未選擇圖片的路徑 selected: '../images/home-a.png' // 這里填寫(xiě)選擇圖片的路徑,這里以vue圖標(biāo)為例 } ] } }, methods: { change(path) { this.$router.push(path) // 這種可以后退 和以下的方法選一即可 // if(this.$router.path === path) return // 無(wú)痕瀏覽 // this.$router.replace(path) } } } </script> <style scoped> .tabBar { position: fixed; width: 100%; height: 70px; left: 0; bottom: 0; display: flex; z-index: 999; } .tabBar ul { width: 100%; display: flex; justify-content: space-around; align-items: center; } .tabBar ul li { display: flex; flex-direction: column; text-align: center; align-items: center; justify-content: space-around; } .tabBar ul li img { width: 30px; height: 100%; margin-bottom: 5px; } .tabBar ul li span { font-size: 15px; } </style>
這里需要注意,圖片需要存入public文件夾的images文件夾內(nèi)部,在路由組件做好相應(yīng)的路由規(guī)則,點(diǎn)擊之后就可以跳轉(zhuǎn)了
文字高亮效果
圖片的高亮效果可以通過(guò)更改路徑來(lái)實(shí)現(xiàn),文字的高亮效果需要邏輯來(lái)實(shí)現(xiàn)
首先定義一個(gè)active的class樣式:
.active { color: red; }
修改li:
<li v-for="(item, index) in list" :key="index" @click="change(item.path)"> <img :src="$route.path.includes(item.path) ? item.selected : item.active" /> <span :class="$route.path.includes(item.path) ? 'active' : ''">{{ item.title }}</span> </li>
在配置路由的入口文件的配置路由添加:
// 配置路由 export default new VueRouter({ linkActiveClass: 'active', // linkExactActiveClass: 'active', // 配置路由 routes: [...] })
linkActiveClass
是模糊匹配linkExactActiveClass
是精準(zhǔn)匹配
這樣兩者的搭配就可以實(shí)現(xiàn)點(diǎn)擊不同區(qū)域的圖片和文字就會(huì)出現(xiàn)高亮的效果
實(shí)例效果,這里稍微修改了一點(diǎn)樣式:
這種效果在PC端多用于導(dǎo)航欄,在移動(dòng)端多用于tabBar,如果是移動(dòng)端我們需要使用rem等系列手法轉(zhuǎn)換單位。
完整代碼
myTabbar.vue的代碼:
<template> <div class="tabBar"> <ul> <li v-for="(item, index) in list" :key="index" @click="change(item.path)"> <img :src="$route.path.includes(item.path) ? item.selected : item.active" /> <span :class="$route.path.includes(item.path) ? 'active' : ''">{{ item.title }}</span> </li> </ul> </div> </template> <script> export default { data() { return { list: [ { title: '首頁(yè)', path: '/home', // 需要跳轉(zhuǎn)的地址 active: '../images/home.png', // 這里填寫(xiě)未選擇圖片的路徑 selected: '../images/home-a.png' // 這里填寫(xiě)選擇圖片的路徑,這里以vue圖標(biāo)為例 }, { title: '分類(lèi)', path: '/demo', active: '../images/home.png', // 這里填寫(xiě)未選擇圖片的路徑 selected: '../images/home-a.png' // 這里填寫(xiě)選擇圖片的路徑,這里以vue圖標(biāo)為例 }, { title: '購(gòu)物車(chē)', path: '/cart', active: '../images/home.png', // 這里填寫(xiě)未選擇圖片的路徑 selected: '../images/home-a.png' // 這里填寫(xiě)選擇圖片的路徑,這里以vue圖標(biāo)為例 }, { title: '我們', path: '/my', active: '../images/home.png', // 這里填寫(xiě)未選擇圖片的路徑 selected: '../images/home-a.png' // 這里填寫(xiě)選擇圖片的路徑,這里以vue圖標(biāo)為例 } ] } }, methods: { change(path) { this.$router.push(path) // 這種可以后退 // if(this.$router.path === path) return // 無(wú)痕瀏覽 // this.$router.replace(path) } } } </script> <style scoped> .tabBar { position: fixed; width: 100%; height: 70px; left: 0; bottom: 0; display: flex; background: #ccc; z-index: 999; } .tabBar ul { width: 100%; display: flex; justify-content: space-around; align-items: center; } .tabBar ul li { display: flex; flex-direction: column; text-align: center; align-items: center; justify-content: space-around; } .tabBar ul li img { width: 30px; height: 100%; margin-bottom: 5px; } .tabBar ul li span { font-size: 15px; } .active { color: red; } </style>
路由入口文件的代碼:
// 配置路由 export default new VueRouter({ ? linkActiveClass: 'active', ? // linkExactActiveClass: 'active', ? // 配置路由 ? routes: [...] })
全局引入的代碼:
// 引入全局組件tabBar import myTabbar from '@/components/myTabbar' Vue.component('myTabbar', myTabbar)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue如何實(shí)現(xiàn)關(guān)閉對(duì)話框后刷新列表
這篇文章主要介紹了vue如何實(shí)現(xiàn)關(guān)閉對(duì)話框后刷新列表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04如何使用vue slot創(chuàng)建一個(gè)模態(tài)框的實(shí)例代碼
這篇文章主要介紹了如何使用vue slot創(chuàng)建一個(gè)模態(tài)框,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05vue3+pinia用戶(hù)信息持久緩存token的問(wèn)題解決
本文主要介紹了vue3+pinia用戶(hù)信息持久緩存token的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07使用this.$nextTick()獲取不到數(shù)據(jù)更新后的this.$refs.xxx.及場(chǎng)景分析
今天遇到了這樣一個(gè)場(chǎng)景,在數(shù)據(jù)更新之后,使用this.$nextTick(()=>{console.log(this.$refs.xxx)}) 獲取不到改dom,但是用setTimeout能夠獲取到,在此記錄一下,感興趣的朋友跟隨小編一起看看吧2023-02-02Vue3的vite中圖片動(dòng)態(tài)加載3種方式
這篇文章主要給大家介紹了關(guān)于Vue3的vite中圖片動(dòng)態(tài)加載3種方式的相關(guān)資料,圖片進(jìn)入可視區(qū)域,進(jìn)行動(dòng)態(tài)加載圖片操作,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11Vue.js 2.0窺探之Virtual DOM到底是什么?
大家可能聽(tīng)說(shuō)Vue.js 2.0已經(jīng)發(fā)布,并且在其中新添加如了一些新功能。其中一個(gè)功能就是“Virtual DOM”。那么下面這篇文章就來(lái)給大家詳細(xì)介紹Vue.js 2.0中的Virtual DOM到底是什么?需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-02-02基于vue監(jiān)聽(tīng)滾動(dòng)事件實(shí)現(xiàn)錨點(diǎn)鏈接平滑滾動(dòng)的方法
本篇文章主要介紹了基于vue監(jiān)聽(tīng)滾動(dòng)事件實(shí)現(xiàn)錨點(diǎn)鏈接平滑滾動(dòng)的方法,非常具有實(shí)用價(jià)值,有興趣的可以了解一下2018-01-01解決vue中修改了數(shù)據(jù)但視圖無(wú)法更新的情況
今天小編就為大家分享一篇解決vue中修改了數(shù)據(jù)但視圖無(wú)法更新的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08