Developing a side panel for navigation

My goal is to create a sidebar that shifts to the right from the left side and makes space on the page when the hamburger menu is pressed. I have made progress in achieving this, but I am encountering difficulties with the 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 pushes the main content to the right so the sidebar doesn't cover it */
.before {
  flex-grow: 1;
  background-color: #a3a3a3;
  transition: flex-grow 1000ms ease;
}

.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>

During the animation, the .before div becomes visible when hiding the sidebar, which is not intended. The issue arises as the .before element shrinks at a slower pace than the sidebar moves to the left, despite both elements having the same animation timing and style.

Answer №1

Sorry, transform: translateX() will solely shift the intended element, without affecting adjacent elements. Nonetheless, you can replicate a similar outcome by utilizing the margin-left property like you demonstrated in your codepen example.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Capturing Screenshots with Ionic Framework

I am currently working on an Ionic application that utilizes geolocation via the Google API. However, I am facing a challenge with implementing a feature where a button in the upper right corner should take a screenshot and trigger a popover with options t ...

Looking to organize my divs by data attributes when clicked - how can I achieve this with javascript?

I am looking to implement a dropdown sorting functionality for multiple divs based on different data attributes such as price and popularity. The specific divs are labeled as "element-1" and are contained within the "board-container". var divList = $(". ...

What could be the reason for my React Component not properly associating with the image?

The title appears to be correctly displayed, but there seems to be an issue with the images. I've thoroughly reviewed the code multiple times, but unfortunately, I'm unable to identify the problem. Please provide guidance on what changes need to ...

Choosing the image that represents your website in Safari web previews

Every time I check iCloud.com on my Safari top sites, the thumbnail is always the same. I'm curious about how I can automate this for my own website. ...

Creating an array of objects data is a breeze with Vue.js

I have an array of data containing selected items, and I need to extract the IDs from this array into a new array so that I can send only the IDs to the back-end. Sample Code method toggleSelection(rows) { console.log('this.multipleSelection : &a ...

Sort the array based on the enum name rather than its value

Below is an example of an enumeration: export enum Foo { AA = 0, ZZ = 1, AB = 2, ER = 5 } In my case, I want to sort my Bars based on the name of the enum (AA, AB, ER, ZZ), rather than the numerical value (0, 1, 2, 5) that they represent. ...

Issue with custom function not being triggered by datepicker onSelect in Internet Explorer with JQuery

I have a datepicker set up like this: $("#startDate").datepicker({ onSelect: changeDate }); This is used with the following input field: <input type="text" id="startDate" value="" class="dateField"/> This setup works well in Chrome, but encou ...

Node.js sends a request to open a new GET method, querying the specific HTML file at "what.should.I.put.here.html/getAll", with the option for

After designing the majority of my website, I decided to host it on a school-based hosting service. My files are organized into two folders: a client folder with my html pages and main.js file, and a server folder containing the API that main.js accesses. ...

Does the AngularJS Controller disappear when the DOM element is "deleted"?

One challenge I encountered is regarding a directive that is connected to an angularjs controller, as shown below: <my-directive id="my-unique-directive" ng-controller="MyController"></my-directive> In the controller, at a certain point, I ne ...

Checking variable length time with JavaScript Regular Expression

My specific requirement is to validate a string ranging from 1 to 4 characters in length (where H stands for hour and M represents minutes): If the string has 1 character, it should be in the format of H (a digit between 0-9). A 2-character string should ...

Tips for retrieving the xpath of elements within a div using python selenium

<div class="col-md-6 profile-card_col"> <span class="label">Company Name :</span> <p class="value">Aspad Foolad Asia</p> </div> For a while now, I've been trying to troublesho ...

Updating Mongoose References

When updating the Kwizz model with the password from req.body and team ID from req.session, it currently replaces the existing teams array. However, I am looking to simply add 1 to the array instead. I have searched for a solution but couldn't find a ...

Tips on prefixing Bootstrap 4 classes to prevent conflicts with CSS classes

Recently, I've been focusing on creating small applications for websites. The websites hosting these apps may or may not use a css framework. To prevent any potential conflicts, I have decided to add a unique prefix to all Bootstrap classes. For insta ...

Preventing scrollbar-induced content shifting in Material-UI components: A guide

My browser app displays content dynamically, which can vary in length compared to the screen size. This causes a vertical scrollbar to appear and disappear on desktop browsers, leading to the content shifting left and right. To prevent this, I apply style ...

The functions Show() and Hide() may not work in all scenarios within jQuery

I'm currently developing a website that allows users to participate in quizzes. Each quiz consists of 20 questions divided into three sections: 1 mark for 10 questions, 2 marks for 5 questions, and 4 marks for 5 questions. For each question, there are ...

Implementing a loop with jQuery in a React application

I encountered an error stating that the i is not defined, even though I have already initialized the npm install jQuery and imported it. Here is a screenshot of the error: https://i.sstatic.net/SV8Ww.png This is my code snippet: isClick ...

Caching of audio files in React streaming is enabled

My dilemma lies in the fact that despite creating a new file in the back-end, the <PlaySound /> component continues to play the old sound file rather than the updated one. Although the sound file maintains the same name and path, its content differs. ...

How to Target HTML Tags Locally using CSS Modules in Next.js?

I am looking to implement smooth scrolling specifically on one page in my Next.js application, such as my blog. Instead of applying it to the entire site through the globals.css file, I need a way to inject scroll-behavior: smooth; into the html tag only f ...

Is it possible to maintain component data in Angular while also incorporating dynamic components?

All the code you need can be found here: https://stackblitz.com/edit/angular-keep-alive-component?file=src/app/app.component.ts Is it possible to maintain the state of entered values when switching components? I am currently utilizing dynamic component r ...

What is the best way to ensure the parent element adjusts to fit its floated children, without exceeding the width of the window?

I am attempting to center a group of floating elements, but due to them being displayed in masonry style, I cannot set them as inline. It is clear that a container is necessary, but I have been unable to find information on how to achieve this: Please dis ...