Implementing CSS transitions across several elements simultaneously can enhance the user experience

There seems to be an issue with animating two elements simultaneously using max-height. The transition always occurs one after the other, with one element completing its transition before the other starts.

setTimeout(function () {
        $(".b").css("max-height", "500px");
    }, 500);

    setTimeout(function () {
        $(".b").css("max-height", "0");
        $(".c").css("max-height", "500px");
    }, 2000);
    

Click here to see this phenomenon in action on js bin

Any assistance in resolving this mystery would be greatly appreciated.

Answer №1

Hey there, do you want it to look like this? It seems like this is what you're looking for.

setTimeout(function () {
  $(".b").css("max-height", "500px");
  $(".c").css("max-height", "500px");
}, 500);


setTimeout(function () {
  $(".b").css("max-height", "0");  
}, 2000);
    .box { background: red; height: 200px; width: 200px; margin: 15px; }
    .a { height: 500px; width: 500px; background: blue; }
    
    .b, .c { max-height: 0; overflow: hidden; transition: max-height 1s; transition-delay: 0; }
  <div class="a">
    
    <div class="b">
      <div class="box"></div>
    </div>
    <div class="c">
      <div class="box"></div>
    </div>
    
  </div>

You can see the demo of this code in action by clicking on the link below:

DEMO

Answer №2

I'm not entirely sure if I've grasped the issue you're facing, but if you're looking to trigger a CSS transition for two elements simultaneously, check out this example

In essence, avoid using two separate setTimeout functions. Instead, execute both CSS transitions within a single setTimeout function like so:

setTimeout(function () {
    $(".element1").css("property", "value");
    $(".element2").css("property", "value");
}, 1000);

Revised Solution

example

HTML

<div class="container">
    <div class="element1"></div>
    <div class="element2"></div>
</div>

CSS

.container {
    height:500px;
    width:500px;
    background:blue;
}
.element1 {
    background: red;
    height: 200px;
    width: 200px;
    margin: 15px;
    transition: height 2s ease;
}
.element2{
     background: red;
    height: 10px;
    width: 200px;
    margin: 15px;
    transition: height 2s ease;
}

JavaScript

setTimeout(function () {
  $(".element1").css("height", "10");
  $(".element2").css("height", "200px");
}, 1000);


setTimeout(function () {
  $(".element1").css("height", "200");
  $(".element2").css("height", "10px");
}, 4000);

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 display list items in rows of three?

I just bought this theme and I'm looking to customize it so that only three items appear in a row on the homepage instead of four. Can this be achieved with CSS or JQuery? Here is the link to the theme: Here is the link Is it possible to use CSS to c ...

How can you leverage JavaScript's map() method to manipulate an element within an array using string functions?

I am attempting to achieve the following. strs = ["one", "two"]; let sorted_str = strs.map((s) => [s.sort(), s]); Basically, my goal is to create a new array of arrays where each nested array consists of the sorted string from the o ...

Trouble with displaying label in PDF post HTML conversion using iText html2pdf

Software Versions: html2pdf v2.0.1 iText 7.1.1 The HTML label that spans the width of the page is as follows: <label class="test">Patient</label> CSS Styling: .test { display: block; font-weight: bold; color: #009fd1; font ...

The azure streak on the mobile dropdown menu

After successfully publishing my website using quarto with R, everything appears fine on desktop and mobile except for a minor issue with drop down menus on iPhone, showing blue lines when clicked. https://i.sstatic.net/xJRmU.jpg At the bottom of my .yml ...

Steps for performing a runtime cast

When working on a web application written in TypeScript, there is a feature where users can add additional JavaScript functions that will be parsed at runtime (new function(Function as String)) for execution. These functions should return an object defined ...

Methods for updating the value of a `<select>` element in an AngularJS controller

Within my HTML code, I have a select element with options ranging from 1 to 10: <select id="selVal" ng-model="product.quantity" ng-options="o as o for o in quantityValues" ng-change="updateDelta(product.quantity, {{product.quantity}}, product.selec ...

Adjusting the navigation image as it passes through various div elements during scrolling

Is it possible to dynamically change an image in the navigation bar based on the user's scroll position? For example, I want pic1 to be displayed when the page content is at the top, then switch to pic2 once the user reaches the footer, and then back ...

Converting milliseconds to a valid date object using Angular form validation

I am facing an issue with form validation and milliseconds in my Angular application. It seems that Angular does not consider time in milliseconds as a valid date format, causing the angular.isDate(1418645071000) function to return false. How can I modify ...

Can you show me the way to open a single card?

Can someone assist me in making it so only one card opens when clicked, rather than all of them opening at once? Additionally, if there is already an open card and I click on another one, the currently open card should close automatically. If you have any ...

Creating a text shadow effect with text that has a transparent color

I am attempting to replicate the design shown below using the CSS text-shadow property, but I keep getting a solid color outcome. I have tried using an rgba value of 255,0,0,0.0 for the font-color and text-shadow like this: text-shadow: -1px -1px 0 #0 ...

When the route is changed, the system must execute a function to verify the authenticity of the token

When routing changes in Angular, I have a requirement to execute a function based on whether a valid token is set or not. Is there anyone who can assist me in achieving this task? In Angular 1, I used to accomplish this using $on. ...

What is the best way to calculate the frequency of a specific word in an array?

I have been attempting to eliminate duplicates from my array and determine the frequency of each word appearing in the array. While I have come across methods to address this issue, none of them seem to be working. For instance, when I input the text "this ...

Different approaches to transforming jQuery code into functional AngularJS code

I am a beginner in AngularJS and I'm looking to implement functionality for a login page similar to the one you see when you click the 'Forgot Password' link: Would it be more appropriate to use a directive instead of a controller for this ...

JavaScript functioning exclusively for specific list items within an unordered list

After implementing JavaScript on my website, I noticed that it only works when clicking on certain list items (li's) and not on others. Specifically, the functionalities for Specialties, Contact Us, and Referral Schemes are not working correctly. ...

Do not start Chart.js on the Y-Axis

As of now, this is the current appearance of the code which includes a Chart.js first image preview. I am seeking advice on how to prevent my data chart from starting at the Y axis. The result of the code can be viewed below. https://i.sstatic.net/sdl3K. ...

Obtaining the "match" object within a Custom Filter Selector on jQuery version 1.8

Here's a helpful resource on Creating a Custom Filter Selector with jQuery for your reference. A Quick Overview: If you're unfamiliar with jQuery's Custom Filter Selectors, they allow you to extend jQuery’s selector expressions by addi ...

Converting Three.js camera rotation.y to a full 360 degrees rotation

Currently, I am attempting to rotate a 2D object in my Three.js scene to correspond with the camera's rotation.y value. However, I have encountered an issue with the radians returned by this value. The rotation seems strange, ranging from 0 to -1.4 ra ...

`How can I dynamically alter the text color of a CSS class using code?`

Currently, I am incorporating Bootstrap.css into my project and utilizing the "form-control" class to style my aspx controls. However, I have encountered an issue where the font color of the textbox is appearing as grey when I desire it to be black. Unfo ...

Differences in jQuery selector behavior when targeting elements in parent windows compared to popup windows

In order for my userscript to function correctly, I have implemented a temporary solution. To create code that is easier to understand and maintain, it's essential for me to gain a deeper understanding of WHY the temporary fix works. Here is some code ...

Continuous Load More: Loads content infinitely until a page is fully loaded

I am currently experimenting with implementing infinite ajax scroll within a Bootstrap modal. Below is the initial appearance of the modal, before any data is loaded: <div class="modal fade" id="modal" tabindex="-1"> <div class="modal-dialog" ...