vue router仿天貓底部導(dǎo)航欄功能
首先把天貓的導(dǎo)航貼出來,里面包括精選、品牌、會員、購物車、我五個導(dǎo)航及對應(yīng)的圖標。

分析:
1、圖標的獲取
進入阿里巴巴矢量圖標庫,網(wǎng)址 http://www.iconfont.cn。
點擊官方圖標庫,選擇天貓圖標庫,選中放入購物車。

點擊添加至項目,點擊創(chuàng)建新項目按鈕,創(chuàng)建tianmao項目,點擊確定。


此時會有查看在線鏈接和下載至本地兩種方式,我選擇第一種,因為后期如果要添加小圖標的話,只需要重新生成在線鏈接,然后更新link即可


復(fù)制鏈接到index.html的link標簽內(nèi),具體為
<link rel="stylesheet" rel="external nofollow" >
引入圖標,使用<i class="icon iconfont icon-wo"></i>區(qū)別在第三個class來引入對應(yīng)圖標
此時所需圖標處理完畢
2、創(chuàng)建精選、品牌、會員、購物車、我及路由導(dǎo)航組件Home.vue、Brand.vue、Member.vue、Cart.vue、Me.vue、Tabs.vue
使用的樣式時less,如果在.vue文件中寫樣式,style必須寫成<style lang="less" type="text/less"></style>,否則會報錯
Tabs.vue
<template>
<div class="tabs">
<!--命名路由-->
<ul>
<!--this inspection reports XML/HTML tags with missing mandatory attrbutes ,you can specify attrbute name that should not be reported-->
<!--home被點擊后,一直處于激活狀態(tài),因此需要使用精確匹配模式,在router-link中添加exact屬性-->
<router-link :to="{name:'Home'}" tag="li" exact>
<div>
<i class="icon iconfont icon-31shouye"></i>
</div>
<div>精選</div>
</router-link>
<router-link :to="{name:'Brand'}" tag="li">
<div>
<i class="icon iconfont icon-zhubaoshipin"></i>
</div>
<div>品牌</div>
</router-link>
<router-link :to="{name:'Member'}" tag="li">
<div>
<i class="icon iconfont icon-huiyuanqia"></i>
</div>
<div>會員</div>
</router-link>
<router-link :to="{name:'Cart'}" tag="li">
<div>
<i class="icon iconfont icon-gouwucheman"></i>
</div>
<div>購物車</div>
</router-link>
<router-link :to="{name:'Me',params:{user:'xu'}}" tag="li">
<div>
<i class="icon iconfont icon-wo"></i>
</div>
<div>我</div>
</router-link>
</ul>
</div>
</template>
<script type="text/ecmascript-6">
export default {}
</script>
<style lang="less" type="text/less">
.tabs {
position: fixed;
bottom: 0;
left: 0;
background-color: #fff;
box-shadow: 0 2px 4px #000;
width: 100%;
& > ul, & > ul > li {
margin: 0;
padding: 0;
}
ul {
display: table;
width: 100%;
& > li {
text-align: center;
font-size: 16px;
display: table-cell;
padding: 8px 12px;
cursor: pointer;
&.router-link-active{
color: #D0021B;
}
& > div {
font-size: 14px;
& > i {
font-size: 30px;
}
}
}
}
}
</style>
我使用的是命名路由,這樣我們就可以當(dāng)路由組件變化時,直接修改router/index.js文件即可。
3、創(chuàng)建路由
router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/Home'
import Brand from '@/Brand'
import Member from '@/Member'
import Cart from '@/Cart'
import Me from '@/Me'
Vue.use(Router)
export default new Router({
//mode: 'history',
//base: __dirname,
//linkActiveClass: 'active', // 更改激活狀態(tài)的Class值
routes: [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/brand',
name: 'Brand',
component: Brand
},
{
path: '/member',
name: 'Member',
component: Member
},
{
path: '/cart',
name: 'Cart',
component: Cart
},
{
path: '/me',
name: 'Me',
component: Me
}
]
})
4、App.vue引入組件Tabs.vue,并添加<router-view>渲染路徑匹配到的視圖組件
<template>
<div id="app">
<Tabs></Tabs>
<div class="content">
<router-view></router-view>
</div>
</div>
</template>
<script>
import Tabs from "./Tabs.vue"
export default {
name: 'app',
data(){
return {}
},
components: {Tabs}
}
</script>
<style>
*{
padding:0;
margin:0;
}
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
</style>
5、導(dǎo)航狀態(tài)樣式
<router-link>對應(yīng)的路由匹配成功后,就會自動設(shè)置class屬性值為router-link-exact-active router-link-active
router-link-exact-active:配置當(dāng)鏈接被精確匹配的時候應(yīng)該激活的CSS類名。
router-link-active:設(shè)置鏈接激活時使用的 CSS 類名。
如果要修改CSS樣式命名,可通過<router-link>屬性exact-active-class和active-class分別設(shè)置,也可通過路由構(gòu)造函數(shù)選項linkExactActiveClass和linkActiveClass來設(shè)置

點擊品牌時展示如下:Home的Tab仍然設(shè)置了routet-link-activeCSS類名,如果設(shè)置routet-link-active樣式顏色為紅色,精選就會一直保持紅色
此時需要使用”精確匹配模式“,<router-link :to="{name:'Home'}" tag="li" exact> 則使用exact,此時的Home的Tab就不會被設(shè)置routet-link-activeCSS類名了
訪問 http://localhost:8080/#/brand 就不會匹配到http://localhost:8080/#/

總結(jié)
以上所述是小編給大家介紹的vue router仿天貓底部導(dǎo)航欄功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- vue實現(xiàn)導(dǎo)航欄效果(選中狀態(tài)刷新不消失)
- vue實現(xiàn)nav導(dǎo)航欄的方法
- vue使用ElementUI時導(dǎo)航欄默認展開功能的實現(xiàn)
- Vue實現(xiàn)導(dǎo)航欄菜單
- Vue實現(xiàn)導(dǎo)航欄點擊當(dāng)前標簽變色功能
- vue elementUI使用tabs與導(dǎo)航欄聯(lián)動
- VUE 實現(xiàn)滾動監(jiān)聽 導(dǎo)航欄置頂?shù)姆椒?/a>
- vue2.0 elementUI制作面包屑導(dǎo)航欄
- vue設(shè)置導(dǎo)航欄、側(cè)邊欄為公共頁面的例子
- vue實現(xiàn)拖動左側(cè)導(dǎo)航欄變大變小
相關(guān)文章
Element?UI表單驗證規(guī)則動態(tài)失效問題的解決辦法
這篇文章主要給大家介紹了關(guān)于Element?UI表單驗證規(guī)則動態(tài)失效問題的解決辦法,Element UI提供了強大的表單驗證功能,可以輕松地對表單進行驗證,需要的朋友可以參考下2023-09-09
vue3項目如何配置按需自動導(dǎo)入API組件unplugin-auto-import
這篇文章主要介紹了vue3項目如何配置按需自動導(dǎo)入API組件unplugin-auto-import問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
詳解利用eventemitter2實現(xiàn)Vue組件通信
這篇文章主要介紹了詳解利用eventemitter2實現(xiàn)Vue組件通信,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

