$('.container').animate({ 'height': el.height() }, { duration: 300});
So el‘s height can be smaller or greater than .container‘s height.
How can I make it so the effect duration lasts longer when the difference between the current height and the one to which I’m animating is larger?
Change the magic constant
5to anything that seems reasonable. You didn’t provide criteria for computing the duration; you might bound it withMath.min(/*above expression*/, maxDuration), or maybe it shouldn’t be linear but logarithmic. You can customize it quite easily. Although that’s a good place to start.