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

后端 未结 8 733
一个人的身影
一个人的身影 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.

    0 讨论(0)
  • 2020-12-07 15:22

    This is actually pretty easy to do. All you have to do is change the css.

    Here's a fiddle with a very simple fade animation: http://jsfiddle.net/elthrasher/sNpjH/

    To make it into a sliding animation, I first had to put my element in a box (that's the slide-container), then I added another element to replace the one that was leaving, just because I thought it would look nice. Take it out and the example will still work.

    I changed the animation css from 'fade' to 'slide' but please note that these are the names I gave it. I could have written slide animation css named 'fade' or anything else for that matter.

    The important part is what's in the css. Here's the original 'fade' css:

    .fade-hide, .fade-show {
        -webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
        -moz-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
        -o-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
        transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
    }
    .fade-hide {
        opacity:1;
    }
    .fade-hide.fade-hide-active {
        opacity:0;
    }
    .fade-show {
        opacity:0;
    }
    .fade-show.fade-show-active {
        opacity:1;
    }
    

    This code changes the opacity of the element from 0 (completely transparent) to 1 (completely opaque) and back again. The solution is to leave opacity alone and instead change the top (or left, if you want to move left-right).

    .slide-hide, .slide-show {
        -webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s;
        -moz-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s;
        -o-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s;
        transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 1.5s;
    }
    .slide-hide {
        position: relative;
        top: 0;
    }
    .slide-hide.slide-hide-active {
        position: absolute;
        top: -100px;
    }
    .slide-show {
        position: absolute;
        top: 100px;
    }
    .slide-show.slide-show-active {
        position: relative;
        top: 0px;
    }
    

    I'm also changing from relative to absolute positioning so only one of the elements takes up space in the container at a time.

    Here's the finished product: http://jsfiddle.net/elthrasher/Uz2Dk/. Hope this helps!

    0 讨论(0)
提交回复
热议问题