vue router仿天貓底部導(dǎo)航欄功能
首先把天貓的導(dǎo)航貼出來,里面包括精選、品牌、會(huì)員、購(gòu)物車、我五個(gè)導(dǎo)航及對(duì)應(yīng)的圖標(biāo)。
分析:
1、圖標(biāo)的獲取
進(jìn)入阿里巴巴矢量圖標(biāo)庫(kù),網(wǎng)址 http://www.iconfont.cn。
點(diǎn)擊官方圖標(biāo)庫(kù),選擇天貓圖標(biāo)庫(kù),選中放入購(gòu)物車。
點(diǎn)擊添加至項(xiàng)目,點(diǎn)擊創(chuàng)建新項(xiàng)目按鈕,創(chuàng)建tianmao項(xiàng)目,點(diǎn)擊確定。
此時(shí)會(huì)有查看在線鏈接和下載至本地兩種方式,我選擇第一種,因?yàn)楹笃谌绻砑有D標(biāo)的話,只需要重新生成在線鏈接,然后更新link即可
復(fù)制鏈接到index.html的link標(biāo)簽內(nèi),具體為
<link rel="stylesheet" rel="external nofollow" >
引入圖標(biāo),使用<i class="icon iconfont icon-wo"></i>
區(qū)別在第三個(gè)class來引入對(duì)應(yīng)圖標(biāo)
此時(shí)所需圖標(biāo)處理完畢
2、創(chuàng)建精選、品牌、會(huì)員、購(gòu)物車、我及路由導(dǎo)航組件Home.vue、Brand.vue、Member.vue、Cart.vue、Me.vue、Tabs.vue
使用的樣式時(shí)less,如果在.vue文件中寫樣式,style必須寫成<style lang="less" type="text/less"></style>,否則會(huì)報(bào)錯(cuò)
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被點(diǎn)擊后,一直處于激活狀態(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>會(huì)員</div> </router-link> <router-link :to="{name:'Cart'}" tag="li"> <div> <i class="icon iconfont icon-gouwucheman"></i> </div> <div>購(gòu)物車</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)路由組件變化時(shí),直接修改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>對(duì)應(yīng)的路由匹配成功后,就會(huì)自動(dòng)設(shè)置class屬性值為router-link-exact-active router-link-active
router-link-exact-active:配置當(dāng)鏈接被精確匹配的時(shí)候應(yīng)該激活的CSS類名。
router-link-active:設(shè)置鏈接激活時(shí)使用的 CSS 類名。
如果要修改CSS樣式命名,可通過<router-link>屬性exact-active-class和active-class分別設(shè)置,也可通過路由構(gòu)造函數(shù)選項(xiàng)linkExactActiveClass和linkActiveClass來設(shè)置
點(diǎn)擊品牌時(shí)展示如下:Home的Tab仍然設(shè)置了routet-link-activeCSS類名,如果設(shè)置routet-link-active樣式顏色為紅色,精選就會(huì)一直保持紅色
此時(shí)需要使用”精確匹配模式“,<router-link :to="{name:'Home'}" tag="li" exact> 則使用exact,此時(shí)的Home的Tab就不會(huì)被設(shè)置routet-link-activeCSS類名了
訪問 http://localhost:8080/#/brand 就不會(huì)匹配到http://localhost:8080/#/
總結(jié)
以上所述是小編給大家介紹的vue router仿天貓底部導(dǎo)航欄功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- vue實(shí)現(xiàn)導(dǎo)航欄效果(選中狀態(tài)刷新不消失)
- vue實(shí)現(xiàn)nav導(dǎo)航欄的方法
- vue使用ElementUI時(shí)導(dǎo)航欄默認(rèn)展開功能的實(shí)現(xiàn)
- Vue實(shí)現(xiàn)導(dǎo)航欄菜單
- Vue實(shí)現(xiàn)導(dǎo)航欄點(diǎn)擊當(dāng)前標(biāo)簽變色功能
- vue elementUI使用tabs與導(dǎo)航欄聯(lián)動(dòng)
- VUE 實(shí)現(xiàn)滾動(dòng)監(jiān)聽 導(dǎo)航欄置頂?shù)姆椒?/a>
- vue2.0 elementUI制作面包屑導(dǎo)航欄
- vue設(shè)置導(dǎo)航欄、側(cè)邊欄為公共頁面的例子
- vue實(shí)現(xiàn)拖動(dòng)左側(cè)導(dǎo)航欄變大變小
相關(guān)文章
Element?UI表單驗(yàn)證規(guī)則動(dòng)態(tài)失效問題的解決辦法
這篇文章主要給大家介紹了關(guān)于Element?UI表單驗(yàn)證規(guī)則動(dòng)態(tài)失效問題的解決辦法,Element UI提供了強(qiáng)大的表單驗(yàn)證功能,可以輕松地對(duì)表單進(jìn)行驗(yàn)證,需要的朋友可以參考下2023-09-09vue3項(xiàng)目如何配置按需自動(dòng)導(dǎo)入API組件unplugin-auto-import
這篇文章主要介紹了vue3項(xiàng)目如何配置按需自動(dòng)導(dǎo)入API組件unplugin-auto-import問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Vue.js 實(shí)現(xiàn)tab切換并變色操作講解
這篇文章主要介紹了Vue.js 實(shí)現(xiàn)tab切換并變色操作講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09Vue拿到二進(jìn)制流圖片如何轉(zhuǎn)為正常圖片并顯示
這篇文章主要介紹了Vue拿到二進(jìn)制流圖片如何轉(zhuǎn)為正常圖片并顯示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06詳解利用eventemitter2實(shí)現(xiàn)Vue組件通信
這篇文章主要介紹了詳解利用eventemitter2實(shí)現(xiàn)Vue組件通信,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11基于Vue.js實(shí)現(xiàn)簡(jiǎn)潔的多屏切換效果
在實(shí)際開發(fā)中,多屏切換是常見的需求,尤其是在需要展示大量?jī)?nèi)容或信息時(shí),下面我將向大家展示我是如何實(shí)現(xiàn)三屏,并通過動(dòng)態(tài)按鈕控制切換屏幕的,感興趣的小伙伴跟著小編一起來看看吧2024-09-09