Home > Web Front-end > CSS Tutorial > How to Animate a Div Element to Auto Height Using jQuery?

How to Animate a Div Element to Auto Height Using jQuery?

Mary-Kate Olsen
Release: 2024-12-07 06:25:19
Original
985 people have browsed it

How to Animate a Div Element to Auto Height Using jQuery?

Animating Element to Auto Height with jQuery

When attempting to animate a

element from a specific height to auto height, issues may arise. To address this, consider the following solution:

  1. Save the current height: Determine the height of the
    prior to resizing.
var curHeight = $('#first').height();
Copy after login
  1. Transition to auto height: Overwrite the height property with 'auto' for a moment.
$('#first').css('height', 'auto');
Copy after login
  1. Obtain the auto height: Fetch the natural height of the
    with auto height.
var autoHeight = $('#first').height();
Copy after login
  1. Revert and animate: Restore the initial height and initiate the animation to the auto height.
$('#first').height(curHeight).animate({height: autoHeight}, 1000);
Copy after login
  1. Concatenate the code: Combine all the steps for a simple solution.
var el = $('#first'),
    curHeight = el.height(),
    autoHeight = el.css('height', 'auto').height();
el.height(curHeight).animate({height: autoHeight}, 1000);
Copy after login

The above is the detailed content of How to Animate a Div Element to Auto Height Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template