CSS animation fails to execute after a class is removed

I'm attempting to smoothly open and close my navigation menu.

When the toggle button is clicked, the .responsive class is either added or removed from the .topnav and .links elements.

My code currently works for the .topnav element, using a height transition for a smooth animation when the .responsive class is toggled.

However, I am facing issues with the .links element. I have tried using transform: translateY(-250px); and top: -250px; for the transition effect.

While these transitions work when the .responsive class is added, there is no animation when the class is removed. How can I resolve this? Thank you.

EDIT: I have tested the code in a JS Fiddle and the basic code is functioning correctly. I will continue to debug my actual code and share the solution once found.

var toggleButton = document.querySelector('.toggle-btn');
var topNav = document.querySelector('.topnav');
var links = document.querySelector('.links');

toggleButton.addEventListener('click', function() {
  topNav.classList.toggle('responsive');
  links.classList.toggle('responsive');
});
.topnav {
  background-color: coral;
  width: 100%;
  height: 65px;
  transition: height 300ms linear;
}

.topnav.responsive { 
  position: relative;
  height: 320px;
}

.links {
  background-color: lemonchiffon;
  position: absolute;
  transition: all 300ms linear;
  top: -300px;
}

.links.responsive {
  transition: all 300ms linear;
  top: 65px;
}
<div class="topnav">
  <span class="toggle-btn">Toggle</span>
  <div class="links">
    <a href="#1">1</a>
    <a href="#2">2</a>
    <a href="#3">3</a>
  </div>
</div>

Answer №1

The solution was found within the HTML formatting.

<div class="topnav">
   <span class="toggle-btn">Toggle</span>
</div>  

<div class="links">
   <a href="#1">1</a>
   <a href="#2">2</a>
   <a href="#3">3</a>
</div>

I had to separate the two classes instead of nesting one within the other.

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

What steps should be taken to incorporate that dynamic sliding element (like a sliding screen paper) on the webpage?

When hovering over the leftmost part of the homepage at www.techants.com, a box shifts to the foreground. I examined the code and noticed a reference to something called screen paper. Does anyone know which script is being used for this effect? How can I ...

Navigating on Blogger can be a tricky task when it comes to searching and

I recently added a Table to my blogger post, along with an input form for readers to easily search data. After finding the code on W3Schools (link here), I implemented it successfully. However, I am looking to make some improvements. Here is my question: ...

Two separate pieces of text converge within a single span

I am facing a challenge with alignment in a bootstrap <span> element. Specifically, I want to align the word "amount" to the left, and the "$" to the right, within the same label. I attempted to nest a <span> inside another <span> and app ...

Shine a light on the input with a captivating outer

Can we replicate the outer glow effect that Safari and other browsers automatically add to an input field without using jQuery? If you want to remove it, check out this article: Thank you! ...

What is the best way to automatically create a line break once the User Input reaches the end of a Textbox?

I've been searching for a solution to this question without any luck. Here is the tag I'm working with: <input type="text" id="userText" class="userTextBox"> And here is my CSS: .userTextBox { /* Add marg ...

Why doesn't the style show up when JavaScript is turned off with Material UI, CSS Modules, and Next.js?

This is my first time diving into Material UI, CSS Modules, and Next.js with a project. Issue: I noticed that when I disable JavaScript in the Chrome DevTools, the styles are not being applied. I'm not sure if this has something to do with Materia ...

I am puzzled by the fact that the center cell of my table is longer than the surrounding cells

I'm currently working on a project that involves creating a webpage. I have a table, and when I execute the code, I notice that the center cell appears longer than the rest of the cells. table,td,tr{border: 1px solid red; } <!document html> ...

Target specifically the onhover div element using JQuery and trigger the smooth sliding down effect with the SlideDown() function

Is it possible to create a unique JQuery slidedown() function that will only be applied to the div where the mouse is hovering? I have managed to make it work by giving each div a separate class, but when I name them all the same, it triggers the slide do ...

Horizontal image scrolling problem across different browsers

Check out my Fiddle. I'm having trouble with scrolling on different browsers. In Chrome, the scroll behaves as expected and stops when the content ends. However, in Firefox, it keeps scrolling even after reaching the end despite setting a fixed width ...

Adjust the height of the second column in Bootstrap 4 textarea to fill the remaining space

I am facing an issue with my layout where I have 2 columns, one containing some inputs and the other only a textarea. The problem is that I want the textarea in the second column to extend and fill the remaining height of the first column, but so far I hav ...

What is the best way to horizontally align two elements that are individually contained within <span> tags?

Is there a way to center a group of CSS buttons on a webpage? Currently, the buttons are aligning to the left even though I have set display: inline-block;. I have found that I can center them by adding an additional parent div and giving it an id with dis ...

What is the best way to reposition an element to the end of its flexbox container?

I have a flexbox-container that contains multiple details-elements with flex-wrap behavior. My goal is to have a details-element move below the other elements within the container when it is opened. For example, I want it to go from this initial layout: ...

How can I resize and horizontally align an image using html/css?

Is it possible to resize, crop and center an image using only HTML/CSS? (Using the img tag or CSS sprite) For instance, if I have a 500x500 pixel image, I would like to resize it to a 250x250 pixel image To make the actual visible image 100x100 pixe ...

Problem involving non-breaking spaces

After changing the font-family of my website from Gotham Pro to Gotham, I encountered an issue with the styling of numbers that were formatted using my currency formatter function. Strangely, when the &nbsp character is removed, everything appears to b ...

Stop elements from expanding by setting an automatic width with no wrapping of white space

Within my HTML page, there are nested divs as follows: div#given -- display: block div#one -- display: table div#two -- display: table-row div#three -- display: block div#four ...

Issue with CSS causing dropdown to open underneath modal

I am facing an issue with my bootstrap modal that contains multiple dropdowns. To accommodate the content, I have set overflow: auto to enable a scroll bar on the right side of the modal. The problem arises when I click on a dropdown - it opens under the ...

Guide to implementing a seamless Vue collapse animation with the v-if directive

Struggling with Vue transitions, I am attempting to smoothly show/hide content using v-if. Although I grasp the CSS classes and transitions involved, making the content appear 'smoothly' using techniques like opacity or translation, once the anim ...

What is the method for showcasing an h1 heading and an unordered list link side by

Can you help me align my h1 and ul elements on the same line in my header? I want to have my logo and navigation links next to each other (This extra text is just filler) Below is the code snippet: HTML: <div id="header"> <h1>Logo</h1> ...

How can I achieve this particular design using flexbox?

Check out the image here: . I'm struggling to understand the examples and live flex configurators. Are they only showing simple examples, or am I just not grasping it? Is it possible for me to use media queries to hide a4 when the screen is less than ...

Can the value in a JavaScript object be updated dynamically when a button is clicked?

In my JavaScript code, there is an object named annualPlan. Whenever a user submits the HTML form for a specific month, I aim to update the value in the object for that particular month accordingly. For instance, if someone submits August 21 and 200, I w ...