Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

bootstrap 4 - How to transition sidebar menu with CSS?

I created sidebar menu that works as expected. Now I would like to add transition to make this look better and more professional. Here is the example of my code:

$('.menu-toggle').on('click', function() {
  $('body').toggleClass('nav-md nav-sm');
});
.sidebar {
  position: absolute;
  top: 35px;
  right: 0px;
  bottom: 0px;
  left: 0px;
  width: 180px;
  background-color: #0071bc;
  color: #fff;
  height: 100%;
  overflow: auto;
  font-size: 9pt !important;
  white-space: nowrap;
}

.sidebar a {
  color: #fff;
}

header.pg-header {
  position: fixed;
  left: 0px;
  top: 0px;
  width: 100%;
  z-index: 3;
  border-bottom: 2px solid #0071bc;
}

header.pg-header .navbar>a.menu-toggle {
  color: #0071bc;
  cursor: pointer;
}

.nav-md .container.body #main-content-area {
  margin-left: 180px;
}

.nav-sm .container.body #main-content-area {
  margin-left: 0px;
}

.nav-sm .container.body .sidebar {
  display: none;
}

body {
  background: #fff;
  font-family: "Helvetica Neue", Roboto, Arial, "Droid Sans", sans-serif;
  font-size: 13px;
  font-weight: 400;
  line-height: 1.471;
  overflow: hidden;
}

body .container.body #main-content-area {
  margin-top: 56px;
  height: 100%;
  overflow: auto;
}

#main-custom-display-zone {
  height: calc(100vh - 140px);
  overflow: auto;
  width: 100%;
  position: relative;
}

#main-container-fluid {
  padding: 0 5px;
}

ul li {
  list-style: none;
}

ul li:after {
  content: "A";
  white-space: pre;
}
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  <title>Single Page App</title>
</head>

<body class="nav-md">
  <div class="container body" id="main-page-wrapper">
    <div class="sidebar">
      <ul>
        <li><a><i class="fa fa-globe" aria-hidden="true"></i> System Parameters</a></li>
        <li><a><i class="fa fa-globe" aria-hidden="true"></i> Select Schema</a></li>
        <li><a><i class="fa fa-globe" aria-hidden="true"></i> Menu Item</a></li>
      </ul>
    </div>

    <header class="pg-header">
      <nav class="navbar navbar-expand-sm bg-white p-1">
        <a class="menu-toggle ml-2 mr-3"><i class="fa fa-bars"></i></a>
      </nav>
    </header>

    <main class="page-content" id="main-content-area">
      <div class="container-fluid">
        <div class="alert alert-success text-center" role="alert">
          Welcome to SPA!
        </div>
      </div>
    </main>
  </div>
</body>

</html>
question from:https://stackoverflow.com/questions/65944815/how-to-transition-sidebar-menu-with-css

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the transition and transform properties to hide and reveal your sidebar. A basic working implementation of that is below, and here's the part that specifically makes it work:

.container.body #main-content-area,
.container.body .sidebar {
  /* these two elements will transition over 300ms when transformed */
  transition: transform .3s;
}

.nav-md .container.body #main-content-area {
  transform: translate(180px);
}

.nav-sm .container.body .sidebar {
  /* the sidebar will be hidden by being 180 pixels offscreen */
  transform: translate(-180px);
}

The CSS I replaced:

.nav-md .container.body #main-content-area {
  margin-left: 180px;
}

.nav-sm .container.body #main-content-area {
  margin-left: 0px;
}

.nav-sm .container.body .sidebar {
  display: none;
}

And the working demo:

$('.menu-toggle').on('click', function() {
  $('body').toggleClass('nav-md nav-sm');
});
.sidebar {
  position: absolute;
  top: 35px;
  right: 0px;
  bottom: 0px;
  left: 0px;
  width: 180px;
  background-color: #0071bc;
  color: #fff;
  height: 100%;
  overflow: auto;
  font-size: 9pt !important;
  white-space: nowrap;
}

.sidebar a {
  color: #fff;
}

header.pg-header {
  position: fixed;
  left: 0px;
  top: 0px;
  width: 100%;
  z-index: 3;
  border-bottom: 2px solid #0071bc;
}

header.pg-header .navbar>a.menu-toggle {
  color: #0071bc;
  cursor: pointer;
}

.container.body #main-content-area,
.container.body .sidebar {
  transition: transform .3s;
}

.nav-md .container.body #main-content-area {
  transform: translate(180px);
}

.nav-sm .container.body .sidebar {
  transform: translate(-180px);
}

body {
  background: #fff;
  font-family: "Helvetica Neue", Roboto, Arial, "Droid Sans", sans-serif;
  font-size: 13px;
  font-weight: 400;
  line-height: 1.471;
  overflow: hidden;
}

body .container.body #main-content-area {
  margin-top: 56px;
  height: 100%;
  overflow: auto;
}

#main-custom-display-zone {
  height: calc(100vh - 140px);
  overflow: auto;
  width: 100%;
  position: relative;
}

#main-container-fluid {
  padding: 0 5px;
}

ul li {
  list-style: none;
}

ul li:after {
  content: "A";
  white-space: pre;
}
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  <title>Single Page App</title>
</head>

<body class="nav-md">
  <div class="container body" id="main-page-wrapper">
    <div class="sidebar">
      <ul>
        <li><a><i class="fa fa-globe" aria-hidden="true"></i> System Parameters</a></li>
        <li><a><i class="fa fa-globe" aria-hidden="true"></i> Select Schema</a></li>
        <li><a><i class="fa fa-globe" aria-hidden="true"></i> Menu Item</a></li>
      </ul>
    </div>

    <header class="pg-header">
      <nav class="navbar navbar-expand-sm bg-white p-1">
        <a class="menu-toggle ml-2 mr-3"><i class="fa fa-bars"></i></a>
      </nav>
    </header>

    <main class="page-content" id="main-content-area">
      <div class="container-fluid">
        <div class="alert alert-success text-center" role="alert">
          Welcome to SPA!
        </div>
      </div>
    </main>
  </div>
</body>

</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...