导航菜单

Bootstrap4滑动侧边栏特效

阿里云

这是一款基于 bootstrap4 的滑动侧边栏特效。该滑动侧边栏使用 bootstrap4 框架,通过 css 和 jquery 来完成隐藏侧边栏,当点击打开侧边栏按钮时,侧边栏会以动画的方式滑动出来。
使用方法:
在页面中引入 jquery 和 bootstrap4 相关文件。

  1. <link href="css/bootstrap.min.css" rel="stylesheet">
  2. <script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
  3. <script src="js/bootstrap.min.js"></script>
也想出现在这里?联系我们
创客主机

HTML 结构:

侧边栏的 HTML 结构如下:

  1. <nav class="navbar navbar-expand-md navbar-dark bg-dark navbar-offcanvas">
  2.   <div class="container-fluid">
  3.       <a class="navbar-brand" href="#">Navbar</a>
  4.       <ul class="navbar-nav navbar-top">
  5.           <li class="nav-item active">
  6.               <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  7.           </li>
  8.           <li class="nav-item">
  9.               <a class="nav-link" href="#">Link</a>
  10.           </li>
  11.       </ul>
  12.       <button class="navbar-toggler navbar-toggler-right navbar-icon" type="button" data-toggle="collapse" data-target="#navbar-mobile" aria-controls="navbar-mobile" aria-expanded="false" aria-label="Toggle navigation">
  13.           <span class="icon-bar bar1"></span>
  14.           <span class="icon-bar bar2"></span>
  15.           <span class="icon-bar bar3"></span>
  16.       </button>
  17.       <div class="navbar-collapse collapse ml-auto" id="navbar-mobile">
  18.           <ul class="navbar-nav ml-auto">
  19.               <li class="nav-image">
  20.                   <img src="logo.png" alt="">
  21.               </li>
  22.               <li class="nav-item">
  23.                   <a href="#!" class="nav-link">Link 1</a>
  24.               </li>
  25.               <li class="nav-item">
  26.                   <a href="#!" class="nav-link">Link 2</a>
  27.               </li>
  28.               <li class="nav-item">
  29.                   <a href="#!" class="nav-link">Link 3</a>
  30.               </li>
  31.           </ul>
  32.       </div>
  33.   </div>
  34. </nav>

页面的内容放置在 wrapperclass 的 div 容器中。

  1. <div class="wrapper">
  2.   ...
  3. </div>

CSS 样式:

为侧边栏添加下面的 CSS 样式。

  1. .wrapper { transition: all 0.5s cubic-bezier(0.685, 0.0473, 0.346, 1); }
  2.  
  3. .navbar {
  4.   height: 75px;
  5.   transition: all .5s .1s;
  6. }
  7.  
  8. .navbar-offcanvas { z-index: 1030; }
  9.  
  10. .navbar-offcanvas .container-fluid {
  11.   position: relative;
  12.   padding: 0;
  13.   transform: translate3d(0px, 0, 0);
  14.   transition: all 0.5s cubic-bezier(0.685, 0.0473, 0.346, 1);
  15. }
  16.  
  17. .navbar-offcanvas .navbar-top { display: none; }
  18. @media (min-width: 992px) {
  19.  
  20. .navbar-offcanvas .navbar-top {
  21.   display: flex;
  22.   margin-left: auto;
  23. }
  24. }
  25.  
  26. .navbar-offcanvas .navbar-top .nav-item {
  27.   margin-right: 22px;
  28.   text-align: center;
  29. }
  30.  
  31. @media (max-width: 991px) {
  32.  
  33. .navbar-offcanvas .navbar-top .nav-item .nav-link { color: #000; }
  34. }
  35.  
  36. .navbar-offcanvas .navbar-toggler {
  37.   padding: 0;
  38.   border: 0;
  39.   outline: none;
  40. }
  41.  
  42. .navbar-offcanvas .navbar-toggler:hover, .navbar-offcanvas .navbar-toggler:focus { cursor: pointer; }
  43.  
  44. @media (min-width: 768px) {
  45.  
  46. .navbar-offcanvas .navbar-toggler { display: block; }
  47. }
  48.  
  49. .navbar-offcanvas .navbar-toggler .icon-bar {
  50.   display: block;
  51.   position: relative;
  52.   width: 24px;
  53.   height: 2px;
  54.   border-radius: 1px;
  55.   background-color: #fff;
  56. }
  57.  
  58. .navbar-offcanvas .navbar-toggler .icon-bar + .icon-bar { margin-top: 4px; }
  59.  
  60. .navbar-offcanvas .navbar-toggler .icon-bar.bar1 {
  61.   top: 0;
  62.   outline: 1px solid transparent;
  63.   animation: topbar-back 500ms 0s;
  64.   animation-fill-mode: forwards;
  65. }
  66.  
  67. .navbar-offcanvas .navbar-toggler .icon-bar.bar2 {
  68.   outline: 1px solid transparent;
  69.   opacity: 1;
  70. }
  71.  
  72. .navbar-offcanvas .navbar-toggler .icon-bar.bar3 {
  73.   bottom: 0;
  74.   outline: 1px solid transparent;
  75.   animation: bottombar-back 500ms 0s;
  76.   animation-fill-mode: forwards;
  77. }
  78.  
  79. .navbar-offcanvas .navbar-toggler.toggled .icon-bar.bar1 {
  80.   top: 6px;
  81.   animation: topbar-x 500ms 0s;
  82.   animation-fill-mode: forwards;
  83. }
  84.  
  85. .navbar-offcanvas .navbar-toggler.toggled .icon-bar.bar2 { opacity: 0; }
  86.  
  87. .navbar-offcanvas .navbar-toggler.toggled .icon-bar.bar3 {
  88.   bottom: 6px;
  89.   animation: bottombar-x 500ms 0s;
  90.   animation-fill-mode: forwards;
  91. }
  92.  
  93. .navbar-offcanvas .navbar-collapse.collapse, .navbar-offcanvas .navbar-collapse.collapse.in, .navbar-offcanvas .navbar-collapse.collapsing { display: none !important; }
  94.  
  95. .nav-open .navbar-collapse { transform: translate3d(0px, 0, 0); }
  96.  
  97. .nav-open .navbar > .container-fluid { transform: translate3d(-424px, 0, 0); }
  98.  
  99. @media (max-width: 991px) {
  100.  
  101. .nav-open .navbar > .container-fluid { transform: translate3d(-282.6666666667px, 0, 0); }
  102. }
  103.  
  104. .nav-open .wrapper { transform: translate3d(-150px, 0, 0); }
  105.  
  106. body > .navbar-collapse {
  107.   display: block !important;
  108.   position: fixed;
  109.   top: 0;
  110.   right: -10px;
  111.   width: 424px;
  112.   height: 100%;
  113.   padding: 60px 1rem;
  114.   background-color: #fff;
  115.   border-left: 1px solid #e3e3e3;
  116.   text-align: center;
  117.   visibility: visible;
  118.   overflow-y: visible;
  119.   transform: translate3d(424px, 0, 0);
  120.   transition: all 0.5s cubic-bezier(0.685, 0.0473, 0.346, 1);
  121.   z-index: 1032;
  122. }
  123.  
  124. body > .navbar-collapse:after {
  125.   content: "";
  126.   position: absolute;
  127.   top: 28px;
  128.   left: -20px;
  129.   border-left: 10px solid #fff;
  130.   border-top: 10px solid transparent;
  131.   border-bottom: 10px solid transparent;
  132.   border-right: 10px solid transparent;
  133.   transform: rotate(180deg);
  134.   transition: all 0.5s cubic-bezier(0.685, 0.0473, 0.346, 1);
  135.   z-index: 1032;
  136. }
  137.  
  138. @media (max-width: 991px) {
  139.  
  140. body > .navbar-collapse { width: 282.6666666667px; }
  141. }
  142.  
  143. body > .navbar-collapse .nav-image { margin-bottom: 65px; }
  144.  
  145. body > .navbar-collapse .nav-image img {
  146.   display: block;
  147.   margin: 0 auto;
  148.   border: 1px solid rgba(0, 0, 0, 0.14);
  149.   width: 91px;
  150.   height: 69px;
  151. }
  152.  
  153. body > .navbar-collapse .navbar-top {
  154.   margin: 0 !important;
  155.   flex-direction: column;
  156. }
  157.  
  158. @media (min-width: 992px) {
  159.  
  160. body > .navbar-collapse .navbar-top { display: none; }
  161. }
  162.  
  163. body > .navbar-collapse .navbar-top li { text-align: center; }
  164.  
  165. body > .navbar-collapse .navbar-top li a {
  166.   display: block;
  167.  padding: .5rem 1rem;
  168.   font-weight: 700;
  169.   color: #000;
  170. }
  171.  
  172. body > .navbar-collapse .navbar-top li a:hover, body > .navbar-collapse .navbar-top li a:focus { text-decoration: none; }
  173.  
  174. body > .navbar-collapse .nav-link, body > .navbar-collapse .dropdown-toggle {
  175.   font-weight: 700;
  176.   color: #222;
  177.   transition: color .2s ease-out;
  178. }
  179.  
  180. body > .navbar-collapse .nav-link:hover, body > .navbar-collapse .nav-link:focus, body > .navbar-collapse .dropdown-toggle:hover, body > .navbar-collapse .dropdown-toggle:focus { text-decoration: none; }
  181.  
  182. body > .navbar-collapse .nav-link.disabled, body > .navbar-collapse .dropdown-toggle.disabled { color: rgba(0, 0, 0, 0.35); }
  183.  
  184. body > .navbar-collapse .nav-link.disabled:hover, body > .navbar-collapse .nav-link.disabled:focus, body > .navbar-collapse .dropdown-toggle.disabled:hover, body > .navbar-collapse .dropdown-toggle.disabled:focus { cursor: not-allowed; }
  185.  @media (min-width: 992px) {
  186.  
  187. body > .navbar-top { display: none; }
  188. }
  189.  
  190. body > #overlay {
  191.   content: "";
  192.   position: fixed;
  193.   top: 0;
  194.   left: auto;
  195.   right: calc(282.6666666667px - 10px);
  196.   width: 100%;
  197.   height: 100%;
  198.   opacity: 0;
  199.   overflow-x: hidden;
  200.   z-index: 1029;
  201. }
  202.  
  203. @media (min-width: 992px) {
  204.  
  205. body > #overlay { right: calc(424px - 10px); }
  206. }
  207.  
  208. @keyframes 
  209. topbar-x {  0% {
  210.  top: 0px;
  211.  transform: rotate(0deg);
  212. }
  213.  45% {
  214.  top: 6px;
  215.  transform: rotate(145deg);
  216. }
  217.  75% {
  218.  transform: rotate(130deg);
  219. }
  220.  100% {
  221.  transform: rotate(135deg);
  222. }
  223. }
  224.  
  225. @keyframes 
  226. topbar-back {  0% {
  227.  top: 6px;
  228.  transform: rotate(135deg);
  229. }
  230.  45% {
  231.  transform: rotate(-10deg);
  232. }
  233.  75% {
  234.  transform: rotate(5deg);
  235. }
  236.  100% {
  237.  top: 0px;
  238.  transform: rotate(0);
  239. }
  240. }
  241.  
  242. @keyframes 
  243. bottombar-x {  0% {
  244.  bottom: 0px;
  245.  transform: rotate(0deg);
  246. }
  247.  45% {
  248.  bottom: 6px;
  249.  transform: rotate(-145deg);
  250. }
  251.  75% {
  252.  transform: rotate(-130deg);
  253. }
  254.  100% {
  255.  transform: rotate(-135deg);
  256. }
  257. }
  258.  
  259. @keyframes 
  260. bottombar-back {  0% {
  261.  bottom: 6px;
  262.  transform: rotate(-135deg);
  263. }
  264.  45% {
  265.  transform: rotate(10deg);
  266. }
  267.  75% {
  268.  transform: rotate(-5deg);
  269. }
  270.  100% {
  271.  bottom: 0px;
  272.  transform: rotate(0);
  273. }
  274. }

CSS 样式:

最后通过下面的 js 代码来完成侧边栏的显示和隐藏。

  1. var window_height;
  2. var window_width;
  3. var navbar_initialized = false;
  4. var nav_toggle;
  5.  
  6. var offCanvas = {
  7.     sidenav: {
  8.         // Sidenav is not visible by default.
  9.         // Change to 1 if necessary
  10.         sidenav_visible: 0
  11.     },
  12.     initSideNav: function initSideNav() {
  13.         if (!navbar_initialized) {
  14.             var $nav = $('nav');
  15.  
  16.             // Add the offcanvas class to the navbar if it's not initialized
  17.             $nav.addClass('navbar-offcanvas');
  18.  
  19.             // Clone relevant navbars
  20.             var $navtop = $nav.find('.navbar-top').first().clone(true);
  21.             var $navbar = $nav.find('.navbar-collapse').first().clone(true);
  22.  
  23.             // Let's start with some empty vars
  24.             var ul_content = '';
  25.             var top_content = '';
  26.  
  27.             // Set min-height of the new sidebar to the screen height
  28.             $navbar.css('min-height', window.screen.height);
  29.  
  30.             // Take the content of .navbar-top
  31.             $navtop.each(function() {
  32.                 var navtop_content = $(this).html();
  33.                 top_content = top_content + navtop_content;
  34.             });
  35.  
  36.             // Take the content of .navbar-collapse
  37.             $navbar.children('ul').each(function() {
  38.                 var nav_content = $(this).html();
  39.                 ul_content = ul_content + nav_content;
  40.             });
  41.  
  42.             // Wrap the new content inside an <ul>
  43.             ul_content = '<ul class="navbar-nav sidebar-nav">' + ul_content + '</ul>';
  44.  
  45.             // Insert the html content into our cloned content
  46.             $navbar.html(ul_content);
  47.             $navtop.html(top_content);
  48.  
  49.             // Append the navbar to body,
  50.             // and insert the content of the navicons navbar just below the logo/nav-image
  51.             $('body').append($navbar);
  52.             $('.nav-image').after($navtop);
  53.  
  54.  
  55.             // Set the toggle-variable to the Bootstrap navbar-toggler button
  56.             var $toggle = $('.navbar-toggler');
  57.  
  58.             // Add/remove classes on toggle and set the visiblity of the sidenav,
  59.             // and append the overlay. Also if the user clicks the overlay,
  60.             // the sidebar will close
  61.             $toggle.on('click', function () {
  62.                 if (offCanvas.sidenav.sidenav_visible == 1) {
  63.                     $('html').removeClass('nav-open');
  64.                     offCanvas.sidenav.sidenav_visible = 0;
  65.                     $('#overlay').remove();
  66.                     setTimeout(function() {
  67.                         $toggle.removeClass('toggled');
  68.                     }, 300);
  69.                 } else {
  70.                     setTimeout(function() {
  71.                         $toggle.addClass('toggled');
  72.                     }, 300);
  73.  
  74.                     // Add the overlay and make it close the sidenav on click
  75.                     var div = '<div id="overlay"></div>';
  76.                     $(div).appendTo("body").on('click', function() {
  77.                         $('html').removeClass('nav-open');
  78.                         offCanvas.sidenav.sidenav_visible = 0;
  79.                         $('#overlay').remove();
  80.                         setTimeout(function() {
  81.                             $toggle.removeClass('toggled');
  82.                         }, 300);
  83.                     });
  84.  
  85.                     $('html').addClass('nav-open');
  86.                     offCanvas.sidenav.sidenav_visible = 1;
  87.                 }
  88.             });
  89.             // Set navbar to initialized
  90.             navbar_initialized = true;
  91.         }
  92.     }
  93. };
  94.  
  95. $(document).ready(function () {
  96.     window_width = $(window).width();
  97.  
  98.     nav_toggle = $('nav').hasClass('navbar-offcanvas') ? true : false;
  99.  
  100.     if (window_width < 992 || nav_toggle) {
  101.         offCanvas.initSideNav();
  102.     }
  103.  
  104.     // Close the sidebar if the user clicks a link or a dropdown-item,
  105.     // and close the sidebar
  106.     $('.nav-link:not(.dropdown-toggle), .dropdown-item').on('click', function () {
  107.         var $toggle = $('.navbar-toggler');
  108.  
  109.         $('html').removeClass('nav-open');
  110.         offCanvas.sidenav.sidenav_visible = 0;
  111.         setTimeout(function () {
  112.             $toggle.removeClass('toggled');
  113.         }, 300);
  114.     });
  115. });
  116.  
  117. $(window).resize(function () {
  118.     window_width = $(window).width();
  119.  
  120.     // More responsive checks if the user resize the browser
  121.     if (window_width < 992) {
  122.         offCanvas.initSideNav();
  123.     }
  124.  
  125.     if (window_width > 992 && !nav_toggle) {
  126.         $('nav').removeClass('navbar-offcanvas');
  127.         offCanvas.sidenav.sidenav_visible = 1;
  128.         navbar_initialized = false;
  129.     }
  130. });

该 bootstrap4 滑动侧边栏特效的 codepen 地址为:https://codepen.io/moso/pen/LjvRep

Bootstrap4 滑动侧边栏特效

已有 1601 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(2)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!