I want to create sidebar which on pressing the hamburger menu translates to right from the left side and the page makes space for it. I’ve created something very close to what I want but I am having trouble with animation
const btnToggleSideBar = document.querySelector(".btn-toggle-sidebar")
const sideBar = document.querySelector(".side-bar");
const containerBefore = document.querySelector(".before");
btnToggleSideBar.onclick = (e) => {
containerBefore.classList.toggle("hide");
sideBar.classList.toggle("hidden");
};
button {
cursor: pointer;
}
.cont-outer {
display: flex;
position: relative;
height: 150px;
width: 550px;
}
.side-bar {
background-color: red;
transition: transform 1000ms ease;
position: absolute;
inset: 0 75% 0 0;
z-index: 1;
}
.side-bar.hidden {
transform: translateX(-100%);
}
.cont-inner {
display: flex;
flex-grow: 1;
}
/* this is here to just push the main content to the right so that the sidebar doesn't cover the main page*/
.before {
flex-grow: 1;
background-color: #a3a3a3;
transition: flex-grow 1000ms ease;
}
/* let main page to take entire space when side bar is moved to the left */
.before.hide {
flex-grow: 0;
}
.main-page {
background-color: blue;
flex-grow: 3;
}
<button class="btn-toggle-sidebar">toggle sidebar</button>
<div class="cont-outer">
<div class="side-bar">
<h4>list of items</h4>
<ul>
<li>apples</li>
<li>oranges</li>
<li>pineapples</li>
</ul>
</div>
<div class="cont-inner">
<div class="before"></div>
<div class="main-page"></div>
</div>
</div>
When you hide the sidebar during animation you can see the .before
div. Which I don’t want to happen. The .before
is shrinking slower than the sidebar is moving to the left even though both the sidebar and .before
div have the same animation-timing
and animation-style