Accordion transition: successfully collapses, but struggles to expand back up

I've been working on creating an accordion that smoothly rolls down when selected and rolls back up when closed. While I've managed to get it to roll down, it simply closes without any transition when I try to close it.

I attempted setting a max-height on the accordion elements and using

transition: max-height 1s ease-in/ease-out
, but unfortunately, it doesn't seem to work when it comes to rolling back up.

As a beginner in web development, I'm struggling to identify my mistakes. Any guidance would be greatly appreciated!

$('.topic-container').on('click', function() {
  if ($(this).hasClass('active')) {
    $(this).removeClass('active')
    $(this).addClass('closed')
  } else {
    $(this).addClass('active')
    $(this).removeClass('closed')
      .siblings()
      .removeClass('active')
      .addClass('closed')
  }
});
.accordion .content {
  position: relative;
  height: 0;
  font-size: 20px;
  text-align: justify;
  width: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  max-height: 0;
  transition: max-height 1s ease;
}

.accordion .topic-container.active .content {
  height: 100%;
  max-height: 1000px;
  transition: max-height 1s ease-in;
}

.accordion .topic-container.closed .content {
  height: 0;
  max-height: 0;
  transition: max-height 1s ease-out;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="accordion-body">
  <div class="accordion">
    <div class="topic-container closed">
      <div class="label">
        Header
      </div>
      <div class="content">
        Inside the accordion tab
      </div>
    </div>
  </div>
</div>

Answer №1

To achieve the desired effect, it is recommended to replace max-height 1s ease-out with

max-height 1s, height 1s ease-out
in the transition property.

.accordion .topic-container.active .content {
    height: 100%;
    max-height: 1000px;
    transition: max-height 1s, height 1s ease-in;
}

.accordion .topic-container.closed .content {
    height: 0;
    max-height: 0;
    transition: max-height 1s, height 1s ease-out;
}

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

Is there a way to adjust the text color based on whether a value is negative?

I am currently developing a web application that performs addition operations on integers. Within this application, I have implemented two functions named num1() and num2() to retrieve the integers provided by the user for calculation. My objective is to m ...

What is the process for applying a filter to a background image within the body of a webpage?

I'm attempting to apply a filter to the background image of the body element. Currently, I have implemented the following code: body { background: url("/img/congruent_pentagon.png"); color: white; font-family: "Raleway"; -webkit-filte ...

What steps can be taken to display database results solely for the user currently logged in and created by them?

Currently, I'm in the midst of a project that involves extracting an HTML list from JSON data using JavaScript. The list is being displayed on the logged-in user's profile, showcasing job listings from the JSON data. While I've successfully ...

Error in .ajax - 'a' is not defined. If 'a' exists, call it, otherwise, display an

I am trying to understand why the following code works: var addresses = {"2-avenue-bir-hakiem": "2 Avenue Bir Hakiem", "56-rue-marcel-pagnol": "56 rue Marcel Pagnol"}; but this code does not work: var addresses = json.val; Even though my JSON output is ...

What is the best way to adjust the size of a background image on Safari

Using Chrome When using Chrome, the functionality is just like what's depicted in the image above. However, when using Safari The issue arises, as it does not work as expected in Safari. See below for the CSS code: background-image: url(r ...

Align dropdown navigation menu/sorting dropdown in the same position as the button

Exploring the world of dropdown menus in CSS has led me to encounter some challenges. I am striving to ensure that the dropdown boxes appear on the same line as the button that triggers them. Below is the CSS code I have been working with: /* global style ...

Storing freshly acquired JSON data via AJAX into an already existing array

I am currently converting the dynamic data being displayed into JSON, then using AJAX to send it to a specific URL. Subsequently, I retrieve the saved JSON data through AJAX and update my display accordingly. However, an error is occurring stating that &a ...

The form submission is failing due to a specific AJAX condition not being met

On my form submission page, I am trying to call a function at the time of form submission, including an AJAX request. The form submission should occur or not based on a condition in the AJAX response message. The AJAX message can have two values: 1 and 0, ...

Utilizing fanybox-2 for creating a variety of background colors

I am currently using fancybox to showcase some of my website links with opaque backgrounds as requested by the designer. I have successfully customized the background color to meet the requirements. However, I am now facing a new question: is it possible ...

The AJAX request returned a 404 error, indicating that the requested resource could

I am attempting to make an AJAX call to a function on the server side. The function is located inside connect_v2.ascx file within the user/module directory. Below is the code I am using for the call: function Request() { $.ajax({ ...

Is it possible to display a subtle grey suggestion within an HTML input field using only CSS?

Have you ever noticed those text input boxes on websites where a grey label is displayed inside the box, but disappears once you start typing? This page has one too: the "Title" field works the same way. Now, let's address some questions: Is ther ...

Completely triggering a forced refresh on a single page application, disregarding cache, service workers, and all other

After experimenting with service workers on my Vue-built website, I made a critical error that has left Safari on my iPhone displaying only a blank page. To troubleshoot the issue, I connected my phone to my Mac and utilized Safari's inspector tool t ...

The jQuery ajax() function may return a null value

I'm attempting to create a simple query: $.ajax({ url: "ajaxfunc.php", dataType: 'json', success: function(data){ alert(data.resp); } }); In my ajaxfunc.php file, the code looks like this: <?php echo ...

The utilization of bootstrap can lead to CSS interruptions

I am trying to create a navigation button that transforms from 3 horizontal lines to an X shape when clicked. var anchor = document.querySelectorAll('button'); [].forEach.call(anchor, function(anchor) { var open = false; an ...

Is there a way to eliminate a specific input box using the jquery remove function?

I've been working on some code and have run into an issue. Currently, the remove function only works on one input box that is created with the append function. I would like it to be able to work on every input box generated through the append function ...

In Angular, a white screen may suddenly appear if the scrolling speed is too fast

My experience has been primarily on Chrome. I've noticed that when I scroll for a long time, the data on the screen disappears briefly and then reappears after a few seconds. Is there a resolution for this problem? Thank you, ...

Choosing a single radio button is simple with these steps

Trying to display radio-buttons in the code below, but encountering an issue where all radio-buttons can be checked simultaneously. The desired functionality is to only allow one radio button to be checked at a time, preventing the selection of multiple op ...

What is the best way to ensure that `col-sm-4` takes up the full space previously occupied by `col-sm-5` when it is hidden

In tablet mode, I am concealing my col-sm-5 using the class hidden-sm. <div class="row"> <div class="col-sm-4"> </div> <div class="col-sm-5"> </div> <div class="col-sm-3"> </div> </div& ...

Accomplishing Nested Element Retrieval in Javascript

While this question may have been asked previously, it hasn't been tackled quite like this before... I have the following block of HTML code. <table class="fr ralign cartSummaryTable"> <tr> <td width="320px"> <div cl ...

The height auto transition in CSS3 begins by shrinking to a height of 0 before expanding to

My CSS code looks like this: .foo { height:100px; overflow:hidden; -webkit-transition: height 200ms; -moz-transition: height 200ms; -o-transition: height 200ms; transition: height 200ms; } .foo.open { height:auto; } When the ...