Comments on: Animate Height/Width to “Auto” https://css-tricks.com Tips, Tricks, and Techniques on using Cascading Style Sheets. Fri, 07 Jul 2017 17:52:16 +0000 hourly 1 https://wordpress.org/?v=6.1.1 By: Tim https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-1610092 Fri, 07 Jul 2017 17:52:16 +0000 http://css-tricks.com/?page_id=15342#comment-1610092 Seems the clone causes some issues due to its positioning in the DOM/CSS DOM. Made a couple small changes to the script to ensure you always get the “auto” height and width by simply using the original element in place. Might be helpful for someone. Oh, also added some logical defaults for prop, speed, and callback.

jQuery.fn.animateAuto = function(prop, speed, callback){
    var beginCache, endCache;

    prop = prop || 'both';
    speed = speed !== null ? 200 : speed;
    callback = callback || function() {};

    return this.each(function(i, el){
        el = jQuery(el);

    // store current height/width dimensions
    beginCache = {width: el.css("width"), height: el.css("height")};

    // set to auto and store width/height dimensions
    el.css('width', 'auto');
    el.css('height', 'auto');
    endCache = {width: el.css('width'), height: el.css('height')};

    // revert back to original dimensions
        el.css(beginCache);

        // animate to auto
        if(prop === "height")
            el.animate({"height":endCache.height}, speed, callback);
        else if(prop === "width")
            el.animate({"width":endCache.width}, speed, callback);  
        else if(prop === "both")
            el.animate(endCache, speed, callback);
    });  
};
]]>
By: Christophe Correia https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-1605112 Mon, 21 Nov 2016 17:56:15 +0000 http://css-tricks.com/?page_id=15342#comment-1605112 Thank you very much for this code. You saved my life

]]>
By: Muhammad Farooq https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-1597264 Mon, 28 Sep 2015 20:46:55 +0000 http://css-tricks.com/?page_id=15342#comment-1597264 wow..
it’s mean.. i’m am the same level of expert :P
I just coded the same thing. and just to know how people are coding to get this result, and I found you .. :) you coded the same ..

does it mean i am a good coder or you are not good like me :)

thanks

]]>
By: Bat https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-1596269 Tue, 04 Aug 2015 13:17:38 +0000 http://css-tricks.com/?page_id=15342#comment-1596269 In reply to Steve.

Love you now.

]]>
By: Joseph https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-1594167 Thu, 30 Apr 2015 01:23:30 +0000 http://css-tricks.com/?page_id=15342#comment-1594167 In reply to Steve.

Steve , you are so awesome!

]]>
By: Jon https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-1593515 Mon, 30 Mar 2015 17:25:00 +0000 http://css-tricks.com/?page_id=15342#comment-1593515 Steve, you’re awesome.

]]>
By: Martin Staflund https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-1587779 Fri, 26 Dec 2014 14:50:08 +0000 http://css-tricks.com/?page_id=15342#comment-1587779 In reply to Wesley.

Hi,

Why clone the element to get height, would it not be simpler (and faster?) to temporarily position (and show) the element way out left in it’s relatively positioned parent? For example by adding a class like this to the element:

.expandMe{
 position:absolute;
 left:-9999px;
 display:block;
}
]]>
By: Trung https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-1587557 Wed, 17 Dec 2014 06:37:13 +0000 http://css-tricks.com/?page_id=15342#comment-1587557 The function animateToAutoHeight work well.
http://stackoverflow.com/questions/5003220/javascript-jquery-animate-to-auto-height

]]>
By: Balamurugan https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-1584407 Thu, 14 Aug 2014 06:30:49 +0000 http://css-tricks.com/?page_id=15342#comment-1584407 Thanks a lot Steve. You saved my day

]]>
By: Caleb https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-1583122 Wed, 25 Jun 2014 14:37:55 +0000 http://css-tricks.com/?page_id=15342#comment-1583122 In reply to Steve.

Wow Steve, you just made my day! I can’t believe I’ve never heard of this method, it works perfectly. Thanks!

]]>
By: Rob https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-1181538 Fri, 21 Feb 2014 21:05:04 +0000 http://css-tricks.com/?page_id=15342#comment-1181538 In reply to Steve.

However, when the height is small, the animation will have a serious delay. Knowing what the height is makes the animation more consistent.

]]>
By: Ben https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-979675 Sun, 29 Dec 2013 06:17:26 +0000 http://css-tricks.com/?page_id=15342#comment-979675 In reply to Steve.

Love it – thanks, Steve!

Here’s some simple jQuery I used with max-width to display a menu in a div with onclick. Keep in mind that any padding on the element you’re adjusting will cause that much of the element to show. Which for my purposes was good (it allowed the ‘«’ and ‘»’ you see in my code to display, which is what the user is clicking to show/hide the menu) but if you don’t want that then be sure to add some extra coding to add/remove your padding.

function toggleHorizontal(hidden_id, click_id){
    if($('#'+hidden_id).css('max-width') == "0px"){
        $("#"+hidden_id).animate({"max-width":"350px"}, 300);
        $("#"+click_id).html('»');
    }else{
        $('#'+hidden_id).animate({"max-width":"0px"}, 300);
        $("#"+click_id).html('«');
    }
}

Then just set the div to max-width: 0px; initially, apply onclick="toggleHorizontal('hidden_id', 'click_id')" and bazinga! Hopefully someone finds this as helpful as I found Steve’s suggestion.

]]>
By: David Clark https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-855009 Mon, 16 Dec 2013 16:36:20 +0000 http://css-tricks.com/?page_id=15342#comment-855009 If anybody finds themselves at the bottom of these comments —- I’m trying to create a reliable solution for this all-too-common problem that’s up on Github, not just in a comment or a blog post.

https://github.com/davidtheclark/jquery.animateAuto

I believe I’ve incorporated the ideas people have shared above, as well as my own — and I’m hoping others want to contribute to developing a totally solid, reliable, and reusable solution. Let’s pool efforts.

]]>
By: Harry https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-801492 Tue, 10 Dec 2013 12:26:25 +0000 http://css-tricks.com/?page_id=15342#comment-801492 In reply to Gray.

A paragraph is a block element.

]]>
By: Austin Crane https://css-tricks.com/snippets/jquery/animate-heightwidth-to-auto/#comment-758589 Wed, 04 Dec 2013 18:25:38 +0000 http://css-tricks.com/?page_id=15342#comment-758589 In reply to Steve.

Had to comment on the EASY approach! Thank you so much!

]]>