您好,登錄后才能下訂單哦!
怎么在bootstrap自定義樣式實現側邊導航欄功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
bootstrap側邊導航欄實現原理
1.側滑欄使用定位fixed
2.使用bootstrap響應式使用工具類 visible-sm visible-xs hidden-xs hidden-sm等對不同屏幕適配
3.側滑欄的側滑效果不使用jquery方法來實現,使用的是css3 transforms屬性進行div的移動,側滑的動畫效果使用的是css屬性transition
4.缺點:使用兩套菜單,一套是pc端屏幕顯示的菜單,一套是移動端顯示的手機導航菜單,這個缺點比較明顯,生成無關的標簽,優點代碼少,簡單容易接受
效果圖
bootstrap側邊導航欄
bootstrap導航欄布局 <!--手機導航欄--> <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導航欄--> <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> <!--手機導航欄側滑--> <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>
一個導航欄的布局,用了兩個導航菜單,一個是pc端的,一個是手機端,利用bootstrap響應式使用工具類visible-xs visible-sm來實現pc端隱藏切換按鈕; visible-lg visible-md 實現了pc端顯示導航欄;visible-xs visible-sm實現手機端顯示手機導航欄。
bootstrap響應式工具類詳見:https://www.runoob.com/bootstrap/bootstrap-responsive-utilities.html
css實現布局和側滑效果(側滑的關鍵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);} /*側滑關鍵*/ .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的兩個屬性:
transform:旋轉div,支持元素2D或3D旋轉,屬性值translateX(X)就是在X軸上移動Xpx的距離
http://www.w3school.com.cn/cssref/pr_transform.asp
而側滑的動畫效果是使用transition屬性,設置屬性的過渡動畫的效果,語法
transition: property duration timing-function delay;
http://www.w3school.com.cn/cssref/pr_transition.asp
單擊事件切換側滑
$("#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) } })
看完上述內容,你們掌握怎么在bootstrap自定義樣式實現側邊導航欄功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。