The CSS transition fails to function properly within an accordion when the submenu is set to 100% width

I have designed a simple accordion menu using jQuery and CSS transition to display submenus. Here is an example:

HTML:

<div class="container">
  <ul class="nk-menu list-unstyled" id="side-menu">
    <li class="nk-menu-item has-sub">
      <a class="nk-menu-link nk-menu-toggle" href="javascript: void(0);">
        <span class="nk-menu-text">First</span>
      </a>
      <ul class="nk-menu-sub">
        <li class="nk-menu-item">
          <a class="nk-menu-link" href="">
            <span>Sub 1</span>
          </a>
        </li>
      </ul>
    </li>
    <li class="nk-menu-item has-sub">
      <a class="nk-menu-link nk-menu-toggle" href="javascript: void(0);">
        <span class="nk-menu-text">Second</span>
      </a>
    </li>
  </ul>
</div>

jQuery:

$(document).on('click', '.nk-menu-toggle', function(e) {
  e.preventDefault();
  let $this = $(this);
  if ($this.parent().hasClass('active')) {
    $this.parent().removeClass('active');
  } else {
    $('.nk-menu').find('li').removeClass('active');
    $this.closest('li').toggleClass('active');
  }
});

CSS:

.nk-menu-sub {
  border-top: 1px solid #2a4163;
  background: #8FBC8F;
  padding: 0px 15px;
  margin: 0 0 7px 0;
  height: 0;
  overflow: hidden;
  z-index: -1;
  position: relative;
  opacity: 0;
  -webkit-transition: .7s ease;
  -moz-transition: .7s ease;
  -o-transition: .7s ease;
  transition: .7s ease;
}

.active>.nk-menu-sub {
  height: 50px; // change this line to 100%
  opacity: 1;
  padding: 15px;
}

In practice, this code works well with a fixed size like 50px for the submenu, but when I try to change the height to 100% for dynamic content in .active>.nk-menu-sub, the CSS transition doesn't function smoothly and there is delay.

How can I fix this issue?

View demo HERE

Answer №1

It is essential to assign a value to the height property in your code for proper functionality. I have made the necessary adjustments to meet your specifications. I trust that you will find it satisfactory.

To view the updated version, please visit the following link: Demo

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 style alternate iframes using CSS?

I need to select every second iframe in the image provided. Is it possible to achieve this using nth-child? I have tried with the following code (unsuccessfully): #main iframe:nth-child(2n+2) ...

Using JavaScript to convert date and time into ISO format

Similar Question: How do I output an ISO-8601 formatted string in Javascript? I've been attempting to change a date and time input into an ISO format but keep encountering the error .toISOString is undefined. It seems like I must be overlooking s ...

What is the encoding for Javascript in UTF-8?

My current ajax call is able to successfully send the request and retrieve the expected response. However, I am facing difficulty in correctly displaying it in li items. $.ajax({ url: "{% url 'users:profile_page_tags_get' 'primary&apos ...

Is it possible to balance proper CSS and Javascript maintenance with the use of a Template Engine?

When using a template engine like Velocity or FreeMaker, you have the ability to break up your HTML into reusable components. For example, if you have an ad <div> that appears on multiple pages of your site, you can create a file containing that < ...

Would this code effectively disable the right-clicking menu for MathJax?

My current approach involves utilizing the following code snippet: <script type="tet/x-mathjax-config"> MathJax.Hub.Config({ showMathMenu: false }); </script> I intended for this code to disable the right-click menu on my math webs ...

Adjusting Position for Parallax Effect using Jquery

I am currently experimenting with a basic Scrolldeck Jquery Parallax effect to scroll items at varying speeds. However, I am encountering some difficulties in making items move from top to bottom. In the example provided below, you will see a shoe moving f ...

Following an ajax load, the binding of click proxy ceases to function

I have been working on a web page that features clickable div elements. When these divs are clicked, corresponding text is displayed elsewhere on the page, which functions correctly. Recently, I implemented an ajax call to dynamically load a new set of di ...

Calling Ajax to Flask for fetching MySQL query results

My goal is to populate a drop-down list in my HTML using AJAX by calling my flask app app.py from app.js. I am attempting to load the data when the page initially loads and triggering the AJAX within $(document).ready as shown below: Here is the content o ...

Having trouble displaying the Order List in Tailwind CSS and Next.js?

I am having trouble displaying an ordered list on my page. I have tried using the <ol> tag, but for some reason the numbers are not showing up. Even when I switch to the <ul> tag, it's still not working properly. Take a look at this scree ...

The div element is experiencing a resizing issue

I am currently working on the following HTML markup: <div class="card" style="height:100%"> <div class="controlPanelContainer"> <div class="card" style="background-color:yellow;height:200px"> </div> <div class= ...

Transform Table header orientation to vertical

I am struggling with a table structure similar to this <table> <thead> <tr> <th>Numbers</th> <th>Alphabet</th> </tr> </thead> <tbody> & ...

What methods are available for drawing on an HTML canvas using a mouse?

I am facing two challenges: how can I create rectangles using the mouse on an HTML canvas, and why is my timer not visible? Despite trying various code snippets, nothing seems to be working. Grateful for any help! HTML: <!DOCTYPE html> <html lan ...

Determine the number of stored values in a specific key by utilizing stringify within localstorage

Is there a way to determine the number of unique values stored under one key in local storage? For instance: Here is the content of my local storage: [{"id":"item-1","icon":"google.com"},{"id":"item-2","icon":"youtube.com"}] In this case, I would like ...

variances in CSS performance between local and remote hosting

My team and I are all using Internet Explorer 8 on Windows 7 Professional, with Visual Studio 2010 Premium. However, we have noticed that when running our application in localhost mode versus remote mode (on the server), there are differences in the CSS re ...

Leveraging JavaScript's Document.write() function to generate an HTML hyperlink

I am struggling with a URL stored in a variable (var URL). My initial attempt was this: document.write("<a href='"+url+"'>LINK</a>"); Unfortunately, it is not working as expected. Any suggestions on how to fix this? This is the ex ...

Incorporate JSON information into a sleek Bootstrap modal interface

I am looking to load a JSON file to generate a list within a Bootstrap Modal. The setup I have allows for the modal to appear when clicking on a person's image. <li class="project span3" data-type="pfa"> <a data-toggle="modal" data-targe ...

What is the best way to update a div whenever there is a change in the database by utilizing jQuery and making an ajax request in PHP

How can I update the content within my div without having to refresh the entire page when there are changes in the database made by other online users? ...

Guide to implement the remember me feature in a jQuery AJAX login system

view: login.php <script> $(document).ready(function(){ $("#login").click(function(e){ e.preventDefault(); elogin = $("#elogin").val(); plogin = $("#plogin").val(); remember_me = $("#remembe ...

Which JQuery plugins can transform regular <select> dropdowns into more stylish options?

After an extensive search, I was only able to locate one solution at this link. I scoured the entire internet for alternatives. ...

Issue with highlighting when one string overlaps with another

I am facing a challenge with handling a string that contains Lorem Ipsum text. I have JSON data that specifies the start and end offsets of certain sections within the text that I need to highlight. The approach I am currently using involves sorting the JS ...