Div that dynamically adjusts its size to fit the content before switching to a fixed size

Apologies for the confusing title - finding it difficult to summarize in just one line!

I have dynamic content loading into divs and a smooth resizing function that animates the height of these divs to auto.

function adjustHeight(div) {

var $selector = $('#' + div);   
$selector.data('originalHeight',$selector.height()).css('height','auto').data('newHeight',$selector.height()).height($selector.data('originalHeight')).animate({height: $selector.data('newHeight')},400);

}

Everything works smoothly in one orientation or viewport size, but when switching to landscape or resizing the browser window on desktop, the divs don't adjust their height correctly causing overflowing content or unnecessary empty space within the div.

Disabling the function makes the divs adjust automatically to fit the content, though losing the appealing animation effect.

Questioning if the issue lies in not setting the height back to auto after the animation, or possibly needing to set the height to zero post animation completion?

Answer №1

give this a shot -

function adjustHeight(container) {
    var $element = $('#' + container);   
    $element.data('originalHeight',$element.height()).css('height','auto').data('newHeight',$element.height()).css('min-height', $element.data('originalHeight')).animate({max-height: $element.data('newHeight')},400);
}

If the above code doesn't solve the issue, please provide a JSFiddle link for further assistance.

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

Refresh the indicator upon data filtering via an ajax request

I'm encountering an issue where the location marker doesn't update when filtering. The old marker remains in place and new markers keep stacking on top of it. Even after using map.removeLayer(), the problem persists. If I use this function after ...

verifying the AJAX validation request URL

I am seeking a way to ensure that the valuable data generated by my ajax code on my webpage can only be accessed when the request originates from my site. This will prevent third parties from using the data without permission. I intend to implement this ...

Can you merge two AJAX functions together?

So I have the following code snippet for AJAX subpages: /******AJAX SUBPAGES ACADEMIES****/ coachesLinks.click(function (e) { e.preventDefault(); }); coachesLinks.click(function (e) { var $el = jQuery(this); var URL = $el.attr( ...

Display a div based on the selection of various radio buttons

I'm in the process of creating a form and would like to include additional fields when a negative response is chosen from a set of three radio buttons. Currently, the code works for a single button, but I want it to apply to any of the three buttons. ...

Returning a button to its original state

Currently, I am working on styling the video controls on my website and have run into a small issue. The video section consists of a video list and a video player. When an li element from the list is clicked, the source of the video player changes to the ...

Can using $.ajax to submit a form be considered contradictory?

Currently, I am engaged in developing an MVC application that heavily relies on jQuery and HTML forms. However, I have started to notice a conflict between the two... For instance, I have had to prevent the default form submission to execute my AJAX call ...

Tips for altering CSS using the `:hover` pseudo-class

CSS Style for Navbar .navbar li { color: #B7B7B7; font-family: 'Montserrat', sans-serif; font-size: 14px; min-width: 70px; padding: 22px 16px 18px; } .navbar #menumain ul { width: 120%; } .navbar ul ul { display: none; } <div ...

Activate the capture property for the file selection based on the label that is selected

This is a form that consists of two labels: <form method="POST" action='/process' enctype="multipart/form-data"> <div> <label for="file" class="upload-button"><i class=" ...

Trouble with Background Image Display in Internet Explorer 8

I have been attempting to set a background image for the . element, but it is not displaying correctly. The image shows up in Firefox and Chrome, but not in Internet Explorer. I have included a link to my website and relevant CSS code below. Can anyone pro ...

What is the most effective method to convert PHP data into JSON and present it in the jQuery success scenario?

In the realm of jQuery, I have this particular piece of code: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script> <script type="text/javascript" > $(function() { $("input[type ...

designing alternating rows within a table

Can I apply styles to alternating rows in an HTML table using only element hierarchy selectors and avoiding style names? I am tasked with styling the HTML output generated by a server component, which does not include styles for alternate rows. While I co ...

Guide to modifying the class color using javascript

I am experiencing an issue with this particular code. I have successfully used getElementById, but when I try to use document.getElementsByClassName("hearts");, it does not work as expected. This is a snippet of my HTML code: status = 1; function change ...

HTML5 text enhanced with a dynamic shadow effect

Struggling with adding a shadow effect to text in my front-end development project. Using HTML5, CSS3, bootstrap, and AngularJS for the design elements, I want to achieve a shadow effect similar to what you would see in Photoshop on a text field. Can anyo ...

Using the <select> element for converting selection from C# to JavaScript

I have a ViewBag.List where I store information about competitions and their respective teams. For example, the list includes the Premier League with teams like Arsenal and Chelsea, as well as the Bundesliga with teams like Bayern Munchen and Wolfsburg. I ...

Guide to dynamically updating a textarea in vue.js by incorporating data from several inputs

Is there a way to update a textarea based on multiple inputs using jQuery and vue.js? I have successfully implemented the jQuery part with line breaks, but when I try to display the value of the textarea elsewhere using vue.js, it doesn't seem to work ...

Using Selenium IDE to click on a checkbox within a table row

Currently, I am utilizing Selenium IDE to navigate through a table with multiple rows and columns. Each row within the table contains its own checkbox for selection purposes. In my attempt to search for a specific row, I have been using the following comm ...

Clicking to remove added child element

I have written a script that creates an image popup when the site loads. Here is the code: <script type="text/javascript"> function showPopup() { var div = document.createElement('div'); div.className += 'popup'; div.inne ...

Tips for persisting form values even after refreshing the page - a guide to setting form values that stay in place

When I submit a long form, an external JavaScript validation is triggered to check the input field validity. If all fields pass validation, a jQuery modal appears prompting the user to either register or log in. If the user chooses to register and complet ...

The Vue 3 page does not allow scrolling unless the page is refreshed

I am encountering an issue with my vue3 web app. Whenever I attempt to navigate to a page using <router-link to ="/Dashboard"/> Here is the code for Dashboard.vue: <template> <div class="enquiry"> <div class= ...

Upon completing the upload, dropzone.js displays checkmark and x icons for confirmation

It seems like there may be a CSS problem. When I programmatically create a dropzone box, I notice that the checkmark and x icons, along with other text, appear after the upload (see image link). <div id="header-dropzone"></div> $("#header-drop ...