Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push Menu - How to push body

I have a menu that opens to the right. My issue is that I want the body to resize after I have opened the menu. The menu is 300px wide and I want the body to resize to take up the remainder of the screen. I saw a few examples, but all they do is shift the body so part of it is not visible and off screen. You can see my example here

<nav class="side-nav hidden">

    <div>
        <div class="open-menu-side" id="side">
            <button class="hamburger hamburger--squeeze" type="button">
              <span class="hamburger-box">
                <span class="hamburger-inner"></span>
              </span>
            </button>
        </div>

        <ul class="side-nav-ul">
            <a href="#"><li class="block">Home</li></a>
            <a href="#"><li class="block">Profile</li></a>
            <a href="#"><li class="block">Blogs</li></a>
            <a href="#"><li class="block">Following</li></a>
            <a href="#"><li class="block">Settings</li></a>
            <a href="#"><li class="block">Logout</li></a>
        </ul>
    </div>
</nav>
<header id="pushed">
    <nav>
        <div class="open-menu" id="main">
            <button class="hamburger hamburger--squeeze" type="button">
              <span class="hamburger-box">
                <span class="hamburger-inner"></span>
              </span>
            </button>
        </div>
        <div class="brand">Login!</div>
    </nav>
</header>
<section></section>


<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
<script type="text/javascript" src="lib/index.js"></script>

``

like image 414
Scott Selke Avatar asked Dec 17 '25 13:12

Scott Selke


1 Answers

As mentioned in the comment, this is what you could do. Add a class to your HTML body when the menu is opened and add a padding-right equals to the width of the menu.

$(".hamburger").on("click", function() {
  $(".hamburger").toggleClass("is-active");
  //add menu-active class to document body
  $('body').toggleClass('menu-active');
  $(".side-nav").toggleClass("hidden");
  if ($("#side").hasClass("is-active")) {
    $("#main").toggleClass("hidden");
  } else if (!$("#side").hasClass("is-active")) {
    $("#main").toggleClass("hidden");
  }
});
html,
body {
  padding: 0;
  margin: 0;
  height: 100%;
  background-color: black;
  box-sizing: border-box;
}

button:focus {
  outline: 0;
}

a {
  color: #fff;
}

a:hover {
  color: #fff;
  text-decoration: none;
}

.side-nav {
  position: absolute;
  background-color: gray;
  width: 300px;
  height: 100%;
  z-index: 1;
  right: 0;
}

.open-menu-side {
  position: relative;
  display: block;
  height: 80px;
  width: 100%;
  text-align: center;
  float: right;
}

.side-nav-ul {
  position: relative;
  display: inline-block;
  width: 100%;
  height: 100%;
  list-style: none;
  font-size: 28px;
  color: #fff;
}

.block {
  height: 40px;
}

header {
  height: 80px;
  background-color: #fff;
}

.brand {
  display: inline-block;
}

.img-menu img {
  height: 50px;
  width: 50px;
  border-radius: 50%;
  border: solid 1px black;
  float: left;
}

.hidden {
  position: absolute;
  right: -300px;
}

#pushed {
  position: relative;
}

#main {
  float: right;
}

.hamburger {
  padding: 15px 15px;
  height: 100%;
  display: inline-block;
  cursor: pointer;
  transition-property: opacity, filter;
  transition-duration: 0.15s;
  transition-timing-function: linear;
  font: inherit;
  color: inherit;
  text-transform: none;
  background-color: transparent;
  border: 0;
  margin: 0;
  overflow: visible;
}

.hamburger:hover {
  opacity: 0.7;
}

.hamburger-box {
  width: 40px;
  height: 24px;
  display: inline-block;
  position: relative;
}

.hamburger-inner {
  display: block;
  top: 50%;
  margin-top: -2px;
}

.hamburger-inner,
.hamburger-inner::before,
.hamburger-inner::after {
  width: 40px;
  height: 4px;
  background-color: #000;
  border-radius: 4px;
  position: absolute;
  transition-property: transform;
  transition-duration: 0.15s;
  transition-timing-function: ease;
}

.hamburger-inner::before,
.hamburger-inner::after {
  content: "";
  display: block;
}

.hamburger-inner::before {
  top: -10px;
}

.hamburger-inner::after {
  bottom: -10px;
}

.hamburger--squeeze .hamburger-inner {
  transition-duration: 0.075s;
  transition-timing-function: cubic-bezier(0.55, 0.055, 0.675, 0.19);
}

.hamburger--squeeze .hamburger-inner::before {
  transition: top 0.075s 0.12s ease, opacity 0.075s ease;
}

.hamburger--squeeze .hamburger-inner::after {
  transition: bottom 0.075s 0.12s ease, transform 0.075s cubic-bezier(0.55, 0.055, 0.675, 0.19);
}

.hamburger--squeeze.is-active .hamburger-inner {
  transform: rotate(45deg);
  transition-delay: 0.12s;
  transition-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
}

.hamburger--squeeze.is-active .hamburger-inner::before {
  top: 0;
  opacity: 0;
  transition: top 0.075s ease, opacity 0.075s 0.12s ease;
}

.hamburger--squeeze.is-active .hamburger-inner::after {
  bottom: 0;
  transform: rotate(-90deg);
  transition: bottom 0.075s ease, transform 0.075s 0.12s cubic-bezier(0.215, 0.61, 0.355, 1);
}

.hello-text {
  text-align: right;
  color: #fff;
  font-size: 22px;
}
/*add padding-right when menu is active*/
body.menu-active {
  padding-right: 300px;
}
<body>
  <nav class="side-nav hidden">
    <div>
      <div class="open-menu-side" id="side">
        <button class="hamburger hamburger--squeeze" type="button">
				  <span class="hamburger-box">
				    <span class="hamburger-inner"></span>
				  </span>
				</button>
      </div>
      <ul class="side-nav-ul">
        <a href="#">
          <li class="block">Home</li>
        </a>
        <a href="#">
          <li class="block">Profile</li>
        </a>
        <a href="#">
          <li class="block">Blogs</li>
        </a>
        <a href="#">
          <li class="block">Following</li>
        </a>
        <a href="#">
          <li class="block">Settings</li>
        </a>
        <a href="#">
          <li class="block">Logout</li>
        </a>
      </ul>
    </div>
  </nav>
  <header id="pushed">
    <nav>
      <div class="open-menu" id="main">
        <button class="hamburger hamburger--squeeze" type="button">
				  <span class="hamburger-box">
				    <span class="hamburger-inner"></span>
				  </span>
				</button>
      </div>
      <div class="brand">Login!</div>
    </nav>
  </header>
  <section></section>

  <p class="hello-text">hello</p>
  <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
  <script type="text/javascript" src="lib/index.js"></script>
</body>

Working codepen https://codepen.io/azs06/pen/KQqqee

like image 192
azs06 Avatar answered Dec 20 '25 05:12

azs06



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!