Moving in a single direction while the class is being removed

I'm attempting to design a sidebar similar to this:

function show_menu(){

  document.querySelector('#sidebar').classList.toggle('sidebar_open');
  document.querySelector('#blackscreen').classList.toggle('blackscreen_open');

}
#hamburger {
  width: 5rem;
  height: 5rem;
  background-color: red;
}

#header_wrapper {
  display: block;
}


#sidebar {
  position: fixed;
  top: 0;
  right: 0;
  width: 0;
  height: 100vh;
  transition: width 0.25s;
}

.sidebar_open {
  width: 50% !important;
  background-color: green;
  display: grid !important;
}
#blackscreen{
position:fixed;
top:0;
right:0;
width:100vw;
height:100vh;
transition: background-color 0.25s;
}

.blackscreen_open {
  background-color: rgba(0, 0, 0, 0.5);
}
    <div id="hamburger" onclick="show_menu()">click here</div>
    <div id='blackscreen' onclick="show_menu()"></div>
    <span id='sidebar'>
    </span>

however, the sidebar transition is only working in one direction,
My assumption is that for the transition to work properly, two classes are needed but since I removed one class, the transition back might not be happening so I attempted the solution from this question like so:

function show_menu(){

  document.querySelector('#sidebar').classList.toggle('sidebar_open');
  document.querySelector('#blackscreen').classList.toggle('blackscreen_open');

}
#hamburger {
  width: 5rem;
  height: 5rem;
  background-color: red;
}

#header_wrapper {
  display: block;
}


#sidebar {
  position: fixed;
  top: 0;
  right: 0;
  width: 0;
  height: 100vh;
}

#sidebar:not(.sidebar_open) {
    transition: width 0.25s;
}

.sidebar_open {
  width: 50% !important;
  background-color: green;
  display: grid !important;
}

#blackscreen {
  position: fixed;
  top: 0;
  right: 0;
  width: 100vw;
  height: 100vh;
  transition: background-color 0.25s;
}

.blackscreen_open {
  background-color: rgba(0, 0, 0, 0.5);
}
    <div id="hamburger" onclick="show_menu()">click here</div>
    <div id='blackscreen' onclick="show_menu()"></div>
    <span id='sidebar'>
    </span>

yet, as you can see, even that approach didn't resolve the issue

I'm fairly certain I'm overlooking something obvious, any assistance will be greatly valued!

Answer №1

I'm pretty confident that the animation is indeed functioning as intended, however, it may not be apparent to you because your sidebar doesn't have a background color when it's closed. Since there isn't a transition specified for the background attribute, removing the sidebar_open class causes the background color to immediately revert to none, making the width transition unnoticeable.

To confirm this, try moving background-color: green; from the .sidebar_open class to the #sidebar element.

Answer №2

Make sure to always have the background-color specified, even if the sidebar is closed. Otherwise, when the class is removed, the content will be invisible despite the width animation.

#sidebar {
  position: fixed;
  top: 0;
  right: 0;
  width: 0;
  height: 100vh;
  background-color: green;
  transition: width 0.25s;
}

function displayMenu() {
  document.querySelector('#sidebar').classList.toggle('menu_open');
  document.querySelector('#overlay').classList.toggle('overlay_open');
}
#hamburger {
  width: 5rem;
  height: 5rem;
  background-color: red;
}

#header_wrapper {
  display: block;
}

#sidebar {
  position: fixed;
  top: 0;
  right: 0;
  width: 0;
  height: 100vh;
  background-color: green;
  transition: width 0.25s;
}

.menu_open {
  width: 50% !important;
  display: grid !important;
}

#overlay {
  position: fixed;
  top: 0;
  right: 0;
  width: 100vw;
  height: 100vh;
  transition: background-color 0.25s;
}

.overlay_open {
  background-color: rgba(0, 0, 0, 0.5);
}
<div id="hamburger" onclick="displayMenu()">Click here</div>
<div id='overlay' onclick="displayMenu()"></div>
<span id='sidebar'></span>

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

Removing the day name from an input date in React without relying on Moment.js

I have successfully implemented code to validate dates, but I am unsure how to retrieve the name of the day for a given date input in the field. For instance: if the date input is 12/01/1994, it should display "Wednesday". function isValidDate(inputDate) ...

Update DIV Background Based on Nearest Radio Selection

Is there a way to make selecting the radio box easier with this code I have? $("div.column43").click(function () { $(this).find('input:radio').attr('checked', true); }); I'm trying to figure out how to change the background and b ...

Execute the function if the text or value begins with a certain character

I'm trying to determine whether the text within a span starts with the letter "x" and then execute a function. I've come across using "^" in strings for this purpose, but am having trouble implementing it to check the text... <span>xfoo&l ...

Setting up a software from a GitHub source

My issue arises when attempting to install a particular package of mine with the version specified as a specific git branch. The problem occurs because the repository does not contain the dist folder, which is required by my npm package. Here is the metho ...

"Unleashing the power of ng-options in AngularJS: a guide to dynamically retrieving the index

I am currently working with a select element, where I need both the selected value and dynamically generated index (I do not want to set the default index to [0]). How can this be achieved? <div class="input-label"> Select Mobile ...

The .find() function conveniently jumps over the table directly following its parent element

After clicking on Inner Add Row, the correct row is added from the .charts-series table. However, when I click on Outer Add Row, a row from the .charts-series table is added instead of one from the .charts table. Here is the link to the example: http://jsf ...

Having trouble with the accordion close animation. It smoothly opens with animation but doesn't close as smoothly, without animation

As someone who is new to javascript, I have a query that may seem basic. I've successfully implemented an open animation, but unfortunately, the close animation isn't functioning as expected. Even after trying to reverse the animation, it doesn& ...

The troubleshooting issue with jQuery animate Scrolltop malfunctioning

My goal is to use jQuery to scroll the page to a specific div when a button is clicked. Despite not seeing any errors in the JavaScript console, the page does not actually scroll to the desired location. I have tried placing the jQuery js file before and a ...

Eliminate the tiny space between the top of the webpage and the navigation bar

I've made several attempts to eliminate the gap between the navigation and the top by setting margin: 0; and border-box in various places to no avail. Strangely, I couldn't get the navigation bar to stick, so I had to resort to making the header ...

Efficient method of delivering cohesive information from database to user without the need for continuous querying

Within the database, there is stored data about each user that typically remains constant. However, occasionally a user may update their information, such as changing their name. This data includes the user's name, username, and company details. The ...

When attempting to convert large objects into JSON using JSON.stringify, a RangeError is thrown due to

Trying to convert a massive JavaScript Object into a string using JSON.stringify in my Node.js application. The objects are extremely large (tens of mega bytes) and do not contain any functions. I need to save the serialized objects to a file. However, I a ...

Creating an image dynamically at runtime from a section of HTML code as it appears in the browser

Is there a way to use Java API or Jquery library to create an image from input HTML code? Alternatively, how can I capture a screenshot of a section of HTML code as it appears in the browser? For example: If I have the following HTML code: <h1>Logo& ...

Strange rendering issues when using the react material-ui dialog

Exploring the Project Recently, I developed a front-end project focusing on a delivery service using React and Material UI. One interesting feature I added was a Dialog window that pops up when users click on an item, allowing them to customize it. Here i ...

In search of the perfect jQuery Autocomplete Control title

If you visit the website , you'll notice a search box in the top-right corner that displays an autocomplete menu while typing. I'm curious to know what this UI Control is called or if there's a library available that offers a similar feature ...

Utilizing memcache in conjunction with PHP and Node.js

Can JavaScript objects and PHP associative arrays be shared with memcache? Alternatively, is it necessary to convert the data into a string before sharing them? ...

How come my SQL query is functioning properly in pgAdmin, but I am encountering a query error (Error: Connection Terminated) when attempting to execute it from the server?

Currently, I am working on my Capstone project which involves storing telemetry data in a PostgreSQL 9.5 database and using node for the server. The issue I am facing is that every time I attempt to send a query from the server, I encounter a query error ...

Real-time Property Availability Widget

I need assistance with creating a website for a homeowner who wants to rent out their property. The client requires a feature that allows them to log in and easily mark individual days as available or unavailable for rental by choosing specific colors for ...

What is the rationale behind Angular 2 and Angular 4's use of banana brackets? Is there a particular reason for this departure from AngularJS's pattern?

Although not directly related to coding, the syntax in AngularJS can still be followed: <input ng-model="hero.name" placeholder="name" type="text"> Here is an example of the syntax used in Angular 2 and its newer versio ...

Eliminate the standard blue border that appears when control-clicking on table elements

I've encountered this question before, but unfortunately none of the solutions provided have worked for me. Some things I've attempted are: Using event.preventDefault() - did not produce the desired result. Removing user-select from CS ...

Resolving conflicting event handlers within vue.js

I have a situation where I'm trying to use two buttons on a page to navigate to different sections. When I include only one button, everything works fine. But when I include both buttons, only one of them functions properly. Upon debugging, I noticed ...