Slide up/down effect with ng-show and ng-animate

后端 未结 8 752
一个人的身影
一个人的身影 2020-12-07 14:40

I\'m trying to use ng-animate to get a behavior similar to JQuery\'s slideUp() and slideDown(). Only I\'d rather use ng-show

8条回答
  •  离开以前
    2020-12-07 15:00

    This can actually be done in CSS and very minimal JS just by adding a CSS class (don't set styles directly in JS!) with e.g. a ng-clickevent. The principle is that one can't animate height: 0; to height: auto; but this can be tricked by animating the max-height property. The container will expand to it's "auto-height" value when .foo-open is set - no need for fixed height or positioning.

    .foo {
        max-height: 0;
    }
    
    .foo--open {
        max-height: 1000px; /* some arbitrary big value */
        transition: ...
    }
    

    see this fiddle by the excellent Lea Verou

    As a concern raised in the comments, note that while this animation works perfectly with linear easing, any exponential easing will produce a behaviour different from what could be expected - due to the fact that the animated property is max-height and not height itself; specifically, only the height fraction of the easing curve of max-height will be displayed.

提交回复
热议问题