Animating the height of a div using nested div elements

Below is the code snippet provided: https://jsfiddle.net/wc48jvgt/69/

The situation involves a div with a horizontal layout named #cleaning_card_1, containing embedded divs that act as borders, and a vertical div called #cleaning_card_right_1 with a nested div #cleaning_card_right_content_1. Within this final div are multiple dynamic amount of divs. The objective is to make #cleaning_card_right_1 function like a dropdown menu. A simple function has been created for this purpose:

function maximizeCleaningCard () {

    var cleaning_card_number = $(this).attr('number')

    $(this).animate({width: '97%'}, 200, function () {
        $('#cleaning_card_right_' + cleaning_card_number).removeClass('cleaning_card_right').addClass('cleaning_card_right_maximized')
        $('#cleaning_card_right_' + cleaning_card_number).animate({height: 400}, 400)
        $('#cleaning_card_right_content_' + cleaning_card_number).animate({height: 400 - 80}, 400)
        $('#cleaning_card_right_left_border_1, #cleaning_card_right_right_border_1').animate({height: 400 - 80}, 400)
    })

}

$('.cleaning_card').click(maximizeCleaningCard) 

While the animation runs smoothly on #cleaning_card_1 and border divs, the issue arises with the animation of #cleaning_card_right_content_1. It appears suddenly without any animation. Removing nested items allows the animation to work correctly. The assumption is that the height of the div is not zero due to the nested elements causing the problem. How can this issue be resolved?

Answer №1

It came to my attention that you are utilizing multiple divs to achieve simultaneous vertical animation. A more efficient approach would be to simplify your div layout by using only 2 animates to accomplish the desired sequence.

I have eliminated unnecessary divs and applied more practical CSS to achieve the same outcome. While it may not be ideal for your final implementation, it should set you in the right direction.

In addition, I have included a minimizing sequence utilizing the class .maximized to indicate the state of the cleaning card and which sequence to execute.

You can also view a live demo on JSFiddle here: https://jsfiddle.net/joshmoto/0ynrm1ts/2/

For detailed information on the functionality, please refer to the comments within the script...

// function to maximize the cleaning card
function maximizeCleaningCard() {

  // store the current clicked cleaning card for later use in animate
  let cleaningCard = this;
  
  // set variable for cleaning card right element
  let cleaningCardRight = $('.cleaning_card_right', cleaningCard);
  
  // check if there is a cleaning card right element
  if($(cleaningCardRight).length) {
    
    // set variable for cleaning card right content
    let cleaningCardRightContent = $('.cleaning_card_right_content', cleaningCardRight);
  
    // if cleaning card has maximized class
    if($(cleaningCard).hasClass('maximized')) {

      // remove the maximized class
      $(cleaningCard).removeClass('maximized');

      // animate the cleaning card right element to close
      $(cleaningCardRight).animate({
          height: '0px'
        }, 500, function() {

          // once cleaning card right is fully closed
          // complete the closing sequence by animating the cleaning card width
          $(cleaningCard).animate({
            width: '70%'
          }, 500 );

        });

    // if cleaning card does not have maximized class
    } else {
      
      // add the maximized class
      $(cleaningCard).addClass('maximized');
      
      // animate the cleaning card width to expand to 100%
      $(cleaningCard).animate({
        width: '100%'
      }, 500, function() {
      
        // once the cleaning card is fully open
        // animate the cleaning card right element to show all content
        $(cleaningCardRight).animate({
          height: $(cleaningCardRightContent).outerHeight()
        }, 500 );

      });

    }
  
  }
  
}

// trigger maximizeCleaningCard function when cleaning card is clicked
$(document).on('click','.cleaning_card', maximizeCleaningCard );
BODY {
  padding: 15px;
  margin: 0;
}

.cleaning_card {
  position: relative;
  width: 70%;
  border: 1px solid black;
  height: 80px;
  font-size: 15px;
  overflow: visible;
  margin-bottom: 15px;
}

.cleaning_card:hover {
  cursor: pointer;
}

.cleaning_card_right {
  position: absolute;
  width: 25%;
  height: 0;
  background-color: white;
  border-bottom: 1px solid black;
  border-left: 1px solid black;
  border-right: 1px solid black;
  overflow: hidden;
  top: 100%;
  right: -1px;
}

.cleaning_card_right_content {
  position: relative;
  padding: 5px;
  overflow: hidden;
}

.cleaning_card_right_content_item {
  position: relative;
  background-color: cornsilk;
}
<div id="cleaning_card_1" class="cleaning_card">

  <div class="cleaning_card_right">

    <div class="cleaning_card_right_content">

      <div class="cleaning_card_right_content_item">
        <div class="cleaning_card_right_category"> Standard </div>
        <div class="cleaning_card_right_value"> 1 </div>
      </div>

      <div class="cleaning_card_right_content_item">
        <div class="cleaning_card_right_category"> Junior Suite </div>
        <div class="cleaning_card_right_value"> 2 </div>
      </div>
      <div class="cleaning_card_right_content_item">
        <div class="cleaning_card_right_category"> Lux </div>
        <div class="cleaning_card_right_value"> 3 </div>
      </div>

    </div>

  </div>

</div>

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

Avoid displaying a popup menu on the webpage during page refresh

How can I prevent a popup menu from appearing again after refreshing the page? I have tried using some javascript but haven't been successful. If anyone has a solution, such as a video tutorial or code snippet, please let me know. ...

Unable to modify src using .attr() on an ng-repeat and ng-click combination is not functioning as expected

I have a collection of pictures (pins) displayed using ng-repeat. One of the requirements is that when a colored pin is clicked, it should turn grey, and vice versa. Here's how my ng-repeat is set up: ng-repeat="note in vm.myData.contactHistoryConta ...

Getting access to the current row along with its value within an AngularJS application

Seeking guidance on how to edit and remove a row in angularjs as I am new to it. In my dynamic table, rows are inserted dynamically with links for editing and deleting. I am looking to edit a row upon clicking the edit link. HTML Code: <div ng-contro ...

How to Use jQuery Post Method to Submit a Form Without Redirecting

I am looking to enhance the user experience of my form by preventing it from reloading upon submission. Specifically, I want to trigger the call to index.php when the submit button named "delete-image" is clicked in the scenario outlined below. It's i ...

Is there a way I can utilize a for-loop and if statement in JavaScript to present the information accurately within the table?

My current task involves fetching data via AJAX and then using a for-loop and if-statement to determine which goods belong in each shopping cart. Once identified, I need to display these goods in separate tables corresponding to each customer. Although the ...

The hidden Bootstrap modal only reveals itself when the button is clicked, staying concealed in the background upon page load

I encountered an issue with a bootstrap modal that was working perfectly in one project, but when I implemented it in another project, it remained hidden in the background and prevented any other modals from overlapping. The modal is visible in the backgro ...

Conceal the scrollbar track while displaying the scrollbar thumb

Hey there! I'm looking to customize my scroll bar to have a hidden track like this. Everyone's got their own style, right? ::-webkit-scrollbar{ width: 15px; height: 40px; } ::-webkit-scrollbar-thumb{ background-color: #DBDBDB; borde ...

Streamlining input options with a single label for multiple selections (radio buttons)

$('label').click(function() { id = this.id.split('-'); if (id[0] === '1') { id[0] = '2'; } else { id[0] = '1'; } $('#' + id[0] + '-' + id[1]).prop('chec ...

What is the best method to modify and access imported .css files within the WordPress style.css file?

I could use some assistance with a project involving minor edits to a WordPress website. I've hit a roadblock and need some help. The style.css file in WordPress doesn't have much CSS code, but instead imports files like this: /*Animate*/ @impo ...

Tips for wrapping the content of a <span> element within a <td> element

Can someone help me figure out how to wrap the content of a <span> tag that is inside a <td>? The style I have applied doesn't seem to be working. Here's the code snippet: <td style="white-space:nowrap;border:1px solid black"> ...

Sending data through a simulated click does not result in transmission to the Nodejs server

Consider the code snippet below: <form action="/map" method="POST" id="submitform"> <input type="text" name="uid" id="UID"/> </form> <button type="submit" id="toSubmit" form="submitform" value="submit" >Submit</button> ...

What are some strategies for blocking URL access to a controller's method in Rails 3?

My goal is to enhance my Rails 3 application by integrating Javascript/jQuery code that retrieves data from the server and updates the page based on the fetched information. I am considering implementing jQuery's $.get() method for this purpose: $.g ...

Guide to creating a dynamic jQuery autocomplete feature with AJAX in a JSP application

I'm currently facing an issue with jQuery UI AutoComplete not working properly when using AJAX in my JSP file. Despite searching through various guides, I haven't been able to find a suitable solution for my problem. Could anyone provide some as ...

How to use jQuery to send a POST request and receive an entire HTML

If I have two pages where one posts a value to another, then makes a MySQL query and returns the data to the first page. If I were to use jQuery JSON post instead of jQuery AJAX post, how can I retrieve the whole page back along with the MySQL query resu ...

Using jQuery AJAX on Windows Azure doesn't automatically redirect and successfully retrieves JSON data from server

I've encountered an issue with my ASP.NET MVC 3 website. While the local version runs smoothly, the login process doesn't function as desired once it's uploaded to the cloud. Below is the code snippet for the login action: [AcceptVerbs(Http ...

The code functions properly on React Webpack when running on localhost, however, it fails to work once deployed to AWS Amplify

As I work on my React.js app, I encountered an issue with hosting pdf files on Amplify. While everything runs smoothly on localhost/3000, allowing me to access and view the pdf files as desired either in a new tab or embedded with html, the same cannot be ...

Angular Material: div not being filled by layout-fill

<!-- Container Div --> <div layout-fill> <!-- Image Div --> <div layout="column" layout-align="center center" class="coverImage" layout-fill> <md-content class="md-padding"> Sample Text Here ...

Encountering a 415 Error in Spring MVC: Unrecognized Media Type Issue

Dealing with a recurring issue and struggling to find a solution that works for me when attempting to post a form using jQuery AJAX. Note: Previously thought it was a client-side issue, tried everything possible on the client side without success. Spr ...

I'm struggling to retrieve and display data from my Flask application using console log. It seems to be a challenge when combining it with

I am encountering an issue while attempting to load data into my Flask page and log it in the console. The error I keep receiving is shown below: VM165:1 Uncaught ReferenceError: data is not defined at <anonymous>:1:13 Below are snippets of cod ...

Concealing Angular Features Using a Button

Currently, I am experimenting with Angular and attempting to create a button that disappears when clicked. Despite trying methods like [hidden], (click)="showHide = !showHide", and several others, nothing seems to be working as expected. This is the curre ...