Search code examples
jqueryanimationhtmlslidetoggle

jQuery slideToggle just shows and hides but doesn't animate correctly


In this example my div container just hides and shows but doesn't do the awesome jQuery animation.

$('.button').click(function() {
    $('.class1').slideToggle();
});

Can anybody say me why? I know that animations on tables don't work correctly but on divs it should be okay.

UPDATE Thanks for all the answers. The information I was missing is that position absolute removes the element from the DOM. And yeah I know I should put the style in a css--this version was just for quick testing, but thanks for the answers!!!


Solution

  • .slideToggle() animates the height of an element from 0% - 100%, hiding its children as it goes.

    By using position:absolute throughout your fiddle, you are removing the child elements from the flow of the DOM and interfering with the ability of the parent element to incrementally hide its children.

    If you set overflow to hidden, and remove position:absolute from the children, this works properly.

    http://jsfiddle.net/7xzMa/6/