移動(dòng)端底部導(dǎo)航固定配合vue-router實(shí)現(xiàn)組件切換功能
在我們平時(shí)練習(xí)或者實(shí)際項(xiàng)目中也好,我們常常遇到這么一個(gè)需求:移動(dòng)端中的導(dǎo)航并不是在頂部也不是在底部,而是在最底部且是固定的,當(dāng)我們點(diǎn)擊該導(dǎo)航項(xiàng)時(shí)會(huì)切換到對(duì)應(yīng)的組件。
相信對(duì)于很多朋友而言,這是一個(gè)很簡(jiǎn)單的需求,而且市面上有很多開源的組件庫就可以實(shí)現(xiàn),像比如說:cube-ui等!那么對(duì)于一個(gè)要是還在練習(xí)以及對(duì)第三方組件庫不是很了解的朋友不妨看看我這篇,相信會(huì)對(duì)你有所收獲的!
首先,在實(shí)現(xiàn)這個(gè)需求之前,我們先分析或者回想下和自己做過的demo中哪個(gè)類似,相信很多朋友立馬就會(huì)想起來---tab欄切換,那么對(duì)于HTML結(jié)構(gòu)的設(shè)計(jì)我們便可以借助tab欄切換的結(jié)構(gòu):一個(gè)大盒子套著兩個(gè)小盒子,一個(gè)作容器,另一個(gè)作導(dǎo)航!
HTML 結(jié)構(gòu)
<div> <div>容器</div> <div class="footer"> <div class="module-nav"> <div class="nav-i"> <div class="iconfont icon"></div> <h3>首頁</h3> </div> <div class="nav-i"> <div class="iconfont icon"></div> <h3>發(fā)現(xiàn)</h3> </div> <div class="nav-i"> <div class="iconfont icon-add"></div> </div> <div class="nav-i"> <div class="iconfont icon"></div> <h3>消息</h3> </div> <div class="nav-i"> <div class="iconfont icon"></div> <h3>我的</h3> <div> </div> </div> </div>
做完HTML結(jié)構(gòu)的編寫,那我們?cè)诮o上面的骨架穿上衣服,根據(jù)需求“底部固定”,我們很容易便會(huì)想到 position: fixed ,當(dāng)然我這里也是用固定定位實(shí)現(xiàn)的,但布局采用的是 flex,在采用 flex 結(jié)合固定定位布局的時(shí)候常常會(huì)出現(xiàn)很多不必要的問題,如:flex 屬性失效,兩者效果沖突等,原因更多的便是“脫標(biāo)”導(dǎo)致的,其中更多的便是出現(xiàn)在父元素 flex,子元素 position的時(shí)候,這時(shí)候可以中間加個(gè)div使兩者擺脫聯(lián)系。
css 樣式( stylus形式 )
.footer position fixed bottom 0 z-index 999 max-width 1080px width 100% border-top 1px solid #C0C0C0 .module-nav display flex justify-content space-around .nav-i width 60px text-align center .icon font-size 35px padding 5px 0 .icon-add font-size 60px h3 font-size 15px font-weight normal margin 0 padding-bottom 5px
骨架和衣服都做好后,那么大概的雛形就出來了,我們的需求也就實(shí)現(xiàn)了一半,剩下的便是組件切換了。這個(gè)就簡(jiǎn)單了,只需要配置下路由表,然后指定跳轉(zhuǎn)便可以了
路由表
routes: [ { path: "/", name: "home", component: Home }, { path: "/find", name: "find", component: Find }, { path: "/info", name: "info", component: Info }, { path: "/user", name: "user", component: User } ]
最后在“容器”內(nèi)添加router-view即可,下面可以看看完整代碼:
// HTML <div> <div class="main-content"> <router-view></router-view> </div> <div class="footer"> <div class="module-nav"> <router-link tag="div" to="/" class="nav-i"> <div class="iconfont icon"></div> <h3>首頁</h3> </router-link> <router-link tag="div" to="/find" class="nav-i"> <div class="iconfont icon"></div> <h3>發(fā)現(xiàn)</h3> </router-link> <div class="nav-i"> <div class="iconfont icon-add"></div> </div> <router-link tag="div" to="/info" class="nav-i"> <div class="iconfont icon"></div> <h3>消息</h3> </router-link> <router-link tag="div" to="/user" class="nav-i"> <div class="iconfont icon"></div> <h3>我的</h3> </router-link> </div> </div> </div> // css .footer position fixed bottom 0 z-index 999 max-width 1080px width 100% border-top 1px solid #C0C0C0 .module-nav display flex justify-content space-around .nav-i width 60px text-align center .icon font-size 35px padding 5px 0 .icon-add font-size 60px h3 font-size 15px font-weight normal margin 0 padding-bottom 5px // router export default new Router({ routes: [ { path: "/", name: "home", component: Home }, { path: "/find", name: "find", component: Find }, { path: "/info", name: "info", component: Info }, { path: "/user", name: "user", component: User } ] });
總結(jié)
以上所述是小編給大家介紹的移動(dòng)端底部導(dǎo)航固定配合vue-router實(shí)現(xiàn)組件切換功能,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
詳解Vue中雙向綁定原理及簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Vue中雙向綁定原理及簡(jiǎn)單實(shí)現(xiàn),文中的示例代碼講解詳細(xì),對(duì)我們深入了解Vue有一定的幫助,需要的可以參考一下2023-05-05vue圖片加載失敗時(shí)用默認(rèn)圖片替換的方法
這篇文章主要給大家介紹了關(guān)于vue圖片加載失敗時(shí)用默認(rèn)圖片替換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue.js中使用echarts實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)刷新功能
這篇文章主要介紹了vue.js中使用echarts實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)刷新功能,需要的朋友可以參考下2019-04-04詳解如何編寫一個(gè)Vue3響應(yīng)式系統(tǒng)
這篇文章主要為大家學(xué)習(xí)介紹了如何編寫一個(gè)Vue3響應(yīng)式系統(tǒng),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-07-07ElementUI實(shí)現(xiàn)在下拉列表里面進(jìn)行搜索功能詳解
有時(shí)候需要用到下拉列表全選和搜索,下面這篇文章主要給大家介紹了關(guān)于ElementUI實(shí)現(xiàn)在下拉列表里面進(jìn)行搜索功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04