bootstrap自定義樣式之bootstrap實(shí)現(xiàn)側(cè)邊導(dǎo)航欄功能
bootstrap自帶的響應(yīng)式導(dǎo)航欄是向下滑動(dòng)的,有時(shí)滿足不了個(gè)性化的需求,需要做一個(gè)類似于android drawerLayout 側(cè)滑的菜單,這就是我要實(shí)現(xiàn)的bootstrap自定義側(cè)滑菜單,參考了很多官網(wǎng)的側(cè)滑,實(shí)現(xiàn)方法各有不同,優(yōu)缺點(diǎn)也十分明顯,有的官網(wǎng)首頁(yè)為了僅僅實(shí)現(xiàn)一個(gè)側(cè)滑的效果,用了owl.carousel滑屏的插件,個(gè)人覺得小題大做了。這個(gè)bootstrap側(cè)滑菜單更專業(yè)的名字叫做手機(jī)導(dǎo)航欄。我也比較這個(gè)名字,更符合bootstrap的特性。所以我這篇文章介紹的更容易的一種做法,新手更容易接受。
bootstrap側(cè)邊導(dǎo)航欄實(shí)現(xiàn)原理
1.側(cè)滑欄使用定位fixed
2.使用bootstrap響應(yīng)式使用工具類 visible-sm visible-xs hidden-xs hidden-sm等對(duì)不同屏幕適配
3.側(cè)滑欄的側(cè)滑效果不使用jquery方法來實(shí)現(xiàn),使用的是css3 transforms屬性進(jìn)行div的移動(dòng),側(cè)滑的動(dòng)畫效果使用的是css屬性transition
4.缺點(diǎn):使用兩套菜單,一套是pc端屏幕顯示的菜單,一套是移動(dòng)端顯示的手機(jī)導(dǎo)航菜單,這個(gè)缺點(diǎn)比較明顯,生成無關(guān)的標(biāo)簽,優(yōu)點(diǎn)代碼少,簡(jiǎn)單容易接受
效果圖
bootstrap側(cè)邊導(dǎo)航欄
bootstrap導(dǎo)航欄布局 <!--手機(jī)導(dǎo)航欄--> <div id="mobile-menu" class="mobile-nav visible-xs visible-sm"> <ul> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁(yè)</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Java</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >SVN</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >iOS</a></li> </ul> </div> <!--pc導(dǎo)航欄--> <nav class="navbar-inverse visible-lg visible-md" role="navigation"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >菜鳥教程</a> </div> <div> <ul class="nav navbar-nav"> <li class="active"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >iOS</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >SVN</a></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="dropdown-toggle" data-toggle="dropdown">Java</a></li> </ul> </div> </div> </nav> <!--手機(jī)導(dǎo)航欄側(cè)滑--> <div class="nav-btn visible-xs visible-sm"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="mobile-nav-taggle" id="mobile-nav-taggle"> <span class="glyphicon glyphicon-align-justify"></span> </a> </div>
一個(gè)導(dǎo)航欄的布局,用了兩個(gè)導(dǎo)航菜單,一個(gè)是pc端的,一個(gè)是手機(jī)端,利用bootstrap響應(yīng)式使用工具類visible-xs visible-sm來實(shí)現(xiàn)pc端隱藏切換按鈕; visible-lg visible-md 實(shí)現(xiàn)了pc端顯示導(dǎo)航欄;visible-xs visible-sm實(shí)現(xiàn)手機(jī)端顯示手機(jī)導(dǎo)航欄。
bootstrap響應(yīng)式工具類詳見:https://www.runoob.com/bootstrap/bootstrap-responsive-utilities.html
css實(shí)現(xiàn)布局和側(cè)滑效果(側(cè)滑的關(guān)鍵css3屬性transform、transition)
代碼不多,僅僅10行
* {margin:0;padding:0;} #mobile-menu {position:fixed;top:0;left:0;width:220px;height:100%;background-color:#373737;z-index:9999;} a:hover ,a:focus{text-decoration:none} .mobile-nav ul li a {color:gray;display:block;padding:1em 5%; border-top:1px solid #4f4f4f;border-bottom:1px solid #292929;transition:all 0.2s ease-out;cursor:pointer;#mobile-menu {position:fixed;top:0;left:0;width:220px;height:100%;background-color:#373737;z-index:9999;transition:all 0.3s ease-in;}} .mobile-nav ul li a:hover {background-color: #23A1F6;color: #ffffff;} .show-nav {transform:translateX(0);} .hide-nav {transform:translateX(-220px);} /*側(cè)滑關(guān)鍵*/ .mobile-nav-taggle {height:35px;line-height:35px;width:35px;background-color:#23A1F6;color:#ffffff;display:inline-block;text-align:center;cursor:pointer} .nav.avbar-inverse{position:relative;} .nav-btn {position:absolute;right:20px;top:20px;}
要值得注意的是css3的兩個(gè)屬性:
transform:旋轉(zhuǎn)div,支持元素2D或3D旋轉(zhuǎn),屬性值translateX(X)就是在X軸上移動(dòng)Xpx的距離
http://www.w3school.com.cn/cssref/pr_transform.asp
而側(cè)滑的動(dòng)畫效果是使用transition屬性,設(shè)置屬性的過渡動(dòng)畫的效果,語法
transition: property duration timing-function delay;
http://www.w3school.com.cn/cssref/pr_transition.asp
單擊事件切換側(cè)滑
$("#mobile-nav-taggle").click(function () { var mobileMenu = $("#mobile-menu"); if (mobileMenu.hasClass("show-nav")) { setTimeout(function () { mobileMenu.addClass("hide-nav").removeClass("show-nav"); }, 100) } else { setTimeout(function (){ mobileMenu.addClass("show-nav").removeClass("hide-nav"); }, 100) } })
總結(jié)
不推薦用兩個(gè)菜單導(dǎo)航欄,缺點(diǎn)很明顯,為了實(shí)現(xiàn)效果而已,不要介意,其實(shí)用一個(gè)菜單導(dǎo)航欄也是可以實(shí)現(xiàn),試試media 完全可以實(shí)現(xiàn)。
代碼下載地址: 點(diǎn)擊此處下載
以上所述是小編給大家介紹的bootstrap自定義樣式之bootstrap實(shí)現(xiàn)側(cè)邊導(dǎo)航欄功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 詳解bootstrap導(dǎo)航欄.nav與.navbar區(qū)別
- Bootstrap實(shí)現(xiàn)漸變頂部固定自適應(yīng)導(dǎo)航欄
- BootStrap學(xué)習(xí)筆記之nav導(dǎo)航欄和面包屑導(dǎo)航
- BootStrap實(shí)現(xiàn)響應(yīng)式布局導(dǎo)航欄折疊隱藏效果(在小屏幕、手機(jī)屏幕瀏覽時(shí)自動(dòng)折疊隱藏)
- 解決bootstrap導(dǎo)航欄navbar在IE8上存在缺陷的方法
- Bootstrap編寫導(dǎo)航欄和登陸框
- Bootstrap3制作自己的導(dǎo)航欄
- Bootstrap導(dǎo)航欄各元素操作方法(表單、按鈕、文本)
- Bootstrap實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄效果
相關(guān)文章
JavaScript自定義Webpack配置實(shí)現(xiàn)流程介紹
本系列主要整理前端面試中需要掌握的知識(shí)點(diǎn)。本節(jié)介紹webpack如何優(yōu)化前端性能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-10-10淺談Emergence.js 檢測(cè)元素可見性的 js 插件
這篇文章主要介紹了淺談Emergence.js 檢測(cè)元素可見性的 js 插件,詳細(xì)的介紹了Emergence.js安裝和使用方法,具有一定的參加性,有興趣的可以了解一下2017-11-11array.prototype.silce.call 理解分析
在很多框架中與遇到這個(gè)方法,但一時(shí)沒去研究這個(gè)方法,只要前段時(shí)間被問到,尷尬無奈收?qǐng)觥K詫W(xué)習(xí)就要追根究底的精神,廢話少說,說正題。2010-04-04div當(dāng)滾動(dòng)到頁(yè)面頂部的時(shí)候固定在頂部實(shí)例代碼
使用Javascript實(shí)現(xiàn)了滾動(dòng)頁(yè)面時(shí),DIV到達(dá)頂部時(shí)固定在頂部。在IE下效果有點(diǎn)閃,具體代碼如下,感興趣的朋友可以參考下哈2013-05-05