Animating the maximum height causes a delay in the performance of other elements

Attempting to adjust the maximum height of a ul. When the max-height property changes (via toggling a class), the height shifts immediately, while the items below the ul maintain the animated height as expected.

var expander = $('.skill-expand');
var skillsUl = $('.skills-list');
expander.on('click', function() {
  skillsUl.toggleClass('unexpanded');
  if (skillsUl.hasClass('unexpanded')) {
    expander.html('<span class="fa fa-chevron-down">..\/..</span>');
  } else {
    expander.html('<span class="fa fa-chevron-up">../\..</span>');
  }
  return false;
});
.skills-list {
  margin: 0;
  padding: 0;
  transition: max-height 1s ease-in-out;
  max-height: 800px;
  /*effectively auto*/
}

.skill-expand {
  margin: auto;
  text-align: center;
  display: block;
}

.skills-list.unexpanded {
  max-height: 60px;
  overflow: hidden;
  transition: max-height 1s ease-in-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="skills-list unexpanded">
  <li class="skill-rating">Developers developers</li>
  <li class="skill-rating">Developers developers</li>
  <li class="skill-rating">Developers developers</li>
  ...
  <li class="skill-rating">Developers developers</li>
  <li class="skill-rating">Developers developers</li>
</ul>

<a href="#" class="skill-expand"><span class="fa fa-chevron-up">..\/..</span></a>

https://jsfiddle.net/ubgmshzo/

After clicking the ..\/.. expander text, observe the delay in the movement of the expander element downwards. Any specific reason for this phenomenon?

Answer №1

By utilizing the jQuery UI toggleClass function, you can set a specific duration and then use a promise to trigger the transition of the up/down chevron icon.

var expander = $('.skill-expand');
var skillsUl = $('.skills-list');

expander.on('click', function() {
  skillsUl.toggleClass('unexpanded', 1000).promise().done(function() {
    if (skillsUl.hasClass('unexpanded')) {
      expander.find('span').removeClass('fa-chevron-down');
      expander.find('span').addClass('fa-chevron-up');
    } else {
      expander.find('span').removeClass('fa-chevron-up');
      expander.find('span').addClass('fa-chevron-down');
    }
  });

  return false;
});
.skills-list {
  margin: 0;
  padding: 0;
  max-height: 800px;
  /*effectively auto*/
}

.skill-expand {
  margin: auto;
  text-align: center;
  display: block;
}

.skills-list.unexpanded {
  max-height: 60px;
  overflow: hidden;
}

.fa-chevron-up:before {
  content: "..\\/..";
}

.fa-chevron-down:before {
  content: "../\\..";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<ul class="skills-list unexpanded">
  <li class="skill-rating">Developers developers</li> // repetitive tasks repeated multiple times
</ul>

<a href="#" class="skill-expand"><span class="fa fa-chevron-up"></span></a>

In addition, I have transferred the styles for the span classes `fa-chevron-up` and `fa-chevron-down` into CSS to simplify toggling without modifying the HTML directly.

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

Switching the sorting criteria from 'AND' to 'OR' in the JPList library

I am currently exploring the use of JPList to sort on multiple items. As it stands, JPList searches like 'AND' when sorting (e.g. sorting based on an item with tags html, php and jQuery all together), but I am interested in filtering through all ...

Investigate the presence of a vertical scrollbar using JQuery or JavaScript

Within an HTML report, I have applied the style "overflow:auto" to allow for dynamic data filling. I am now facing the challenge of detecting whether a vertical scrollbar exists within the report. After scouring various forums for solutions, I came across ...

Developing and integrating views within a node-webkit desktop application

For my file copier desktop application built with node webkit, I aim to create a seamless flow where the initial check for existing profile data determines the first page displayed. The header with static links/buttons to various views remains consistent ...

Django plugin designed for showing a real-time feed of messages - powered by Dajax or Jquery?

Currently, I am attempting to set up a section in my Django application where updates or messages from the server can be displayed once specific tasks are done. I had initially looked into using a plugin that utilizes Dajax / Jquery for this feature, but ...

Hiding elements with CSS using the display:none property and using the :

Trying to display an image when hovering over another image. Works well in Safari, but Chrome and Firefox fail to load the image properly. Have searched for solutions related to visibility:hidden but none seem to solve this cross-browser issue. HTML being ...

Steps to create a clickable drop-down arrow with a label

I've designed a vertical navigation accordion menu that allows users to hover and click on any title (label) to expand a sub-menu. However, I'm facing an issue where my drop-down arrow is not clickable, unlike the rest of the label which works f ...

Transform Typescript into compiled css files without using any additional tools

Currently, I am working on a monorepo project where the main project relies on another project called components. When running the entire monorepo, the main project utilizes webpack.dev while the components project simply uses the TypeScript compiler. It l ...

Having trouble getting a dropdown menu to function properly with jQuery

Within my project, I have implemented a menu that transforms into a dropdown menu for smaller screens. Below is the HTML code for this functionality: <select> <option value="index.php" id="home">Home</option> <option value="about ...

Is there a way to showcase my information on flash cards using JavaScript?

Currently, I am developing a full stack application that utilizes JavaScript on both the front and back end. This application allows users to create their own flashcards set. Upon clicking "View Cards," the data is fetched and the question-answer pair is d ...

Updating the functionality of one-page scrolling - changing to overlap instead of sliding upwards

Hello, I am currently utilizing a JavaScript library to create a website with full page scrolling. If you want to know about the library, check out this link: For those interested, here is an example of my work on jsfiddle: http://jsfiddle.net/aLjLjxux/ ...

Retrieve the HTTP Code and Response Header for a successful AJAX request

I am attempting to retrieve the HTTP Response Code/Response Header from my AJAX request. Below is the initial script I used: $("#callContact1").click(function() { $.ajax({ url: "https://www.server.com?type=makecall", data: {}, type: ...

Issue with Color Attribute in Heading within an Ionic Content Element

The color attribute doesn't seem to be working in Ionic 2. It works fine with normal Ionic tags, but not with HTML tags. Can someone help me with this? The code is provided below. <ion-content> <div class="page-home"> <h1 color="second ...

Guide to positioning an image at the center across 2 columns using Bootstrap

Looking to create a split screen Bootstrap 4 layout with an image in the center overlapping the two columns. Have successfully set up two equal columns but struggling to position the image to overlap. Have experimented with position:absolute and float:lef ...

Attempting to cycle through rows of a table and triggering setInterval for each row does not appear to be successful

I am attempting to change the value of a cell from Offline to Online (and vice versa) within my DataTable by utilizing a web service. var $myTable = $("#my-table").dataTable(); var $myTableRows = $myTable.fnGetNodes(); for(var i=0; i<$myTableRows.leng ...

How can I code a div to automatically scroll to the top of the page?

I'm attempting to implement something similar in WordPress - I want a div containing an advertisement to initially start 300px down the page, then scroll up with the page until it reaches the top, and finally stay there if the user continues to scroll ...

Tips for customizing CSS for the ValidatorCallout Extender in an ajax tool

I am currently attempting to implement the ValidatorCallout Extender feature from the asp.net Ajax website. However, I am struggling with customizing the CSS for the popup validator. ...

The process of uploading a file to the server directory via ajax is not functioning correctly. Despite attempts to upload the image, it does not successfully transfer to the designated upload

I am encountering an issue with uploading files to the server upload directory using AJAX. The image is not being successfully uploaded and I keep receiving a "connection reset" error. Can you please review my code below to see if there are any mistakes? T ...

Preventing jQuery from altering document.location

Looking to fulfill a long-time request in just 2 minutes with a 1ms timeout, along with AJAX calls every 5 seconds for a project I'm working on. In order to cancel all AJAX calls and change the document location with the click of a button, I've ...

Ways to access input fields in a form without knowing the specific name

I am facing a challenge with a dynamically generated form using jQuery Ajax, where the input field names and values are also dynamic. I need to figure out how to update these fields using PHP. One issue I encounter is that upon form submission, I am unsur ...

Using Laravel 4 to dynamically set images using jQuery

I have a section where I display all the images stored on my server (the URL and additional information for each photo are saved in my database). Here is the layout of my image gallery: <div class="row myImages" id="uploadedImages"> @foreach($image ...