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

后端 未结 8 781
一个人的身影
一个人的身影 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:21

    This class-based javascript animation works in AngularJS 1.2 (and 1.4 tested)

    Edit: I ended up abandoning this code and went a completely different direction. I like my other answer much better. This answer will give you some problems in certain situations.

    myApp.animation('.ng-show-toggle-slidedown', function(){
      return {
        beforeAddClass : function(element, className, done){
            if (className == 'ng-hide'){
                $(element).slideUp({duration: 400}, done);
            } else {done();}
        },
        beforeRemoveClass :  function(element, className, done){
            if (className == 'ng-hide'){
                $(element).css({display:'none'});
                $(element).slideDown({duration: 400}, done);
            } else {done();}
        }
    }
    

    });

    Simply add the .ng-hide-toggle-slidedown class to the container element, and the jQuery slide down behavior will be implemented based on the ng-hide class.

    You must include the $(element).css({display:'none'}) line in the beforeRemoveClass method because jQuery will not execute a slideDown unless the element is in a state of display: none prior to starting the jQuery animation. AngularJS uses the CSS

    .ng-hide:not(.ng-hide-animate) {
        display: none !important;
    }
    

    to hide the element. jQuery is not aware of this state, and jQuery will need the display:none prior to the first slide down animation.

    The AngularJS animation will add the .ng-hide-animate and .ng-animate classes while the animation is occuring.

提交回复
热议问题