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)首頁為了僅僅實(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)代碼少,簡單容易接受
效果圖

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" >首頁</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生成GUID的實(shí)現(xiàn)示例
GUID(全局統(tǒng)一標(biāo)識(shí)符)是指在一臺(tái)機(jī)器上生成的數(shù)字,下面為大家介紹下原生JavaScript生成GUID的實(shí)現(xiàn),需要的朋友不要錯(cuò)過2014-09-09
javascript對(duì)數(shù)組的常用操作代碼 數(shù)組方法總匯
js下數(shù)組也是比較常用的,對(duì)數(shù)組元素的處理也是有非常多的方法,這里整理了下,需要的朋友可以參考下。2011-01-01
利用Javascript開發(fā)一個(gè)二維周視圖日歷
這篇文章主要給大家介紹了關(guān)于利用Javascript如何開發(fā)一個(gè)二維周視圖日歷的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
php實(shí)例分享之實(shí)現(xiàn)顯示網(wǎng)站運(yùn)行時(shí)間
這篇文章主要介紹了php實(shí)現(xiàn)顯示網(wǎng)站運(yùn)行時(shí)間,需要的朋友可以參考下2014-05-05
js中各種時(shí)間日期格式之間的轉(zhuǎn)換代碼示例
這篇文章主要介紹了js中各種時(shí)間日期格式之間轉(zhuǎn)換的相關(guān)資料,還有獲取今天的年月日和計(jì)算兩個(gè)日期之間的相差天數(shù)的方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
JavaScript實(shí)現(xiàn)多重繼承的方法分析
這篇文章主要介紹了JavaScript實(shí)現(xiàn)多重繼承的方法,結(jié)合實(shí)例形式詳細(xì)分析了javascript實(shí)現(xiàn)多重繼承的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
詳細(xì)聊聊JavaScript是如何影響DOM樹構(gòu)建的
DOM (Document Object Model) 的全稱是文檔對(duì)象模型,它可以以一種獨(dú)立于平臺(tái)和語言的方式訪問和修改一個(gè)文檔的內(nèi)容和結(jié)構(gòu),這篇文章主要給大家介紹了關(guān)于JavaScript是如何影響DOM樹構(gòu)建的相關(guān)資料,需要的朋友可以參考下2021-08-08

