Is there a way to implement a modal resize animation using JavaScript, jQuery, or CSS?

Bootstrap is being used to showcase a modal dialog, with jQuery handling the dynamic display and hiding of certain elements within the modal. The issue at hand is that whenever an item is shown or hidden, the modal immediately resizes. The desired outcome is for it to smoothly transition to the new size using either a jQuery or CSS animation.

An attempt was made to achieve this with CSS by using the following code:

.modal {
    flex-grow: 1;
    -webkit-transition: all 300ms ease-in-out;
    -moz-transition: all 300ms ease-in-out;
    -o-transition: all 300ms ease-in-out;
    transition: all 300ms ease-in-out;
}

Unfortunately, this approach did not work as expected.

Below is an example of the modal currently in use:

<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
           <p>A paragraph</p>
        </div>
    </div>
</div>

Your assistance on this matter is greatly appreciated. Thank you!

Answer №1

It seems that animating an element when new content is added using CSS or JavaScript is quite challenging. I have not been able to achieve it with CSS alone yet. The issue lies in the browser immediately rendering the new content and updating the auto-height element, which does not trigger CSS transitions. This same problem persists even when using JavaScript. A jQuery solution from this answer might be helpful:

I believe this could be the solution you are seeking.

$('#add-content').on('click', function() {
  $('<p>More Content</p>').css({display: 'none'}).appendTo(('.modal-body')).show('slow');
});
.modal-body {
overflow:hidden;
  transition:transform 19s ease-out; // Note the use of transform instead of height!
  height:auto;
  transform:scaleY(1); // Implicit behavior but specifying explicitly is beneficial
  transform-origin:top; // Optional - keeps the top of the element unchanged
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong">
  Launch demo modal
</button>



<!-- Modal -->
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p id="content">...</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary" id="add-content">Add Content</button>

      </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

Tips for customizing the background of form inputs in React-Bootstrap

Currently, I have a next.js project with react-bootstrap. I am attempting to change the default blueish gray background color (#e8f0fe or rgb(232, 240, 254)) that bootstrap uses for form inputs to white. To achieve this, I am customizing the bootstrap var ...

Unable to return data to HTML page

I'm utilizing EJS to pass some data to my webpage. Here's the snippet of code: app.post('/ttt', function (req,res){ res.render('index.ejs', {titles: 'CAME IN'}) }); HTML <form id="mc-form" action="http://loc ...

The removeAttribute function has the ability to remove the "disabled" attribute, but it does not have the capability to remove

When it comes to my JavaScript code, I have encountered an issue with two specific lines: document.getElementsByName('group')[0].removeAttribute('disabled'); document.getElementsByName('group')[0].removeAttribute('checke ...

Eliminating border from CSS button

I am facing an issue with my buttons in a drop down menu displaying a black outline around each button, which I would like to remove to achieve a more integrated look with the background. Alternatively, I am looking to make the outline white so that it is ...

Guide on displaying a Custom 2D shape on both sides using three.js

As a beginner to three.js and 3D programming in general, I recently used three.js to draw a sector. However, I am facing an issue where I can only see the object in one direction but not in the opposite direction. It appears that the same phenomenon is h ...

Automatically advance to the next input field and go back when the backspace key is pressed after reaching the maximum length

Creating a credit card input field that switches cursor after reaching maximum number length and focuses on previous field when deleting with backspace. (function ($) { $(".nmipay-input-field").keyup(function () { if (this.value.l ...

How come justify-content-center aligns the content to the center of a row, but not a container?

Why is the content centered in this code snippet? <div class="row justify-content-center"> <h2>Some heading</h2><br> </div> And why isn't it centered in this one? <div class="container justify-conten ...

What could be the reason for the jquery click event not working?

When viewing default.aspx, you can click on the + symbol to increase the quantity and the - symbol to decrease the quantity. <div class="sp-quantity"> <div class="sp-minus fff"> ...

Achieving Text Centering in kableExtra Cells with CSS: A Step-by-Step Guide

Recently, I received a helpful solution that involved using the extra_css = argument in cell_spec() from the kableExtra package. This allowed me to fill the entire cell of my table instead of just the text inside it. However, even after specifying align = ...

The intricacies of converting AudioBuffers to ArrayBuffers

I currently have an AudioBuffer stored on the client-side that I would like to send through AJAX to an express server. This link provides information on how a XMLHttpRequest can handle sending and receiving binary data in the form of an ArrayBuffer. In m ...

Is the utilization of react-router for developing 'single-page applications' causing a depletion of server resources?

Hello, I am relatively new to the world of web development and would appreciate guidance on any misunderstandings I may have. Currently, I am immersing myself in learning the MERN stack. For my practice project, I am aiming to create a simple two-page webs ...

Steps for dynamically adjusting form fields depending on radio button selection

I am looking to create a dynamic form that changes based on the selection of radio buttons. The form includes textfields Field A, Field B, ..., Field G, along with radio buttons Radio 1 and Radio 2. I need to update the form dynamically depending on which ...

What is the best way to extract information from a dynamically generated input field using vanilla JavaScript?

Creating a dynamic input field inside the 'education-field' div is the main goal here. The user can add more fields by clicking the "Add more" button, and the data from these input fields is crucial for form validation purposes. Users can input t ...

"Selecting multiple options is not possible as the size attribute cannot be utilized

My <select multiple="multiple">..</select> element is set to a height of 1-row due to the select{heigth: 30px;} style in an inaccessible stylesheet. The "size" attribute does not work in this case. What can I do to fix this issue? ...

Ways to utilize a React custom hook that returns a value within the useEffect hook?

I'm dealing with a custom hook that returns a value based on the parameters it receives. In order to update this value dynamically, I attempted to use useEffect but encountered issues as the hook cannot be called within it. How can I work around this ...

Tips for ending a setInterval loop

I have created a setInterval function in JavaScript to switch the color of a div by applying different CSS classes. Initially, I trigger this color change by pressing a button. However, I am also trying to stop the color transition using the same button bu ...

Dealing with exceptions raised by struts2 when invoking actions through ajax

Utilizing the jquery post method, I am calling the save method within the DepartmentAction. If a duplicate department is attempted to be saved (which is not allowed in the database), the save method will throw an Exception. However, the user does not recei ...

Encountering issues with managing CometD channels within Angular 2

After dabbling in Angular2 and Typescript, I decided to challenge myself by creating an application using plain javascript with the CometD library. The goal of this app was to retrieve data from a CometD channel and present it to the user in some way. So, ...

Establish a table containing rows derived from an object

I am currently dealing with a challenge in creating a table that contains an array of nested objects. The array I have follows this schema: array = [ { id: 'Column1', rows: { row1: 'A', row2 ...

Tips on organizing a two-dimensional array based on the values in a specific column?

I could use some assistance with sorting a 2D array in JavaScript. The array will be structured like this: [12, AAA] [58, BBB] [28, CCC] [18, DDD] After sorting, it should appear like this: [12, AAA] [18, DDD] [28, CCC] [58, BBB] In essence, I need to ...