Search code examples
angularjsng-animate

How to show a fade animate using nganimate when ui-router changes route?


.ng-leave {
    transition: 1s linear all;
    opacity: 1;
}

.ng-leave-active {
    opacity: 0;
}

Accrording to above code, every time router changes route from any page(page1.html) to another page(page2.html), there will be fade animation. But, if I want to have a different leave animation for different views, how can I accomplish that?


Solution

  • You could add different transitions to different views by adding the animations to the view child elements rather than the view element itself.

    I've just posted a tutorial with a working demo showing a couple of different transitions using ngAnimate with ui router, including a slide in/out transition that adds animations to the view child elements.

    Here's the snippet from my LESS file that animates the view child elements:

    /* side form */
    .view-side-form {
        .side-form {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
    
            .content {
                position: absolute;
                z-index: 100;
                top: 0;
                right: 0;
                width: 80%;
                height: 100%;
                overflow: auto;
                background: #fff;
                padding: 20px;
                border-left: 1px solid #e0e0e0;
            }
    
            .background {
                background: #000;
                opacity: .8;
                width: 100%;
                height: 100%;
            }
        }
    
    
        /* TRANSITION ANIMATIONS FOR SIDE FORM VIEW
        ------------------------------------------*/
    
        /* animations for side form view */
        &.ng-enter, 
        &.ng-leave {
            /* transition on enter and leave for .5s */
            transition: .5s;
        }
    
        /* start 'enter' transition */
        &.ng-enter {
            .side-form {
                .content {
                    /* start with content 80% off the right of the page */
                    right: -80%;
                }
    
                .background {
                    /* start with background opacity 0 (invisible) */
                    opacity: 0;
                }
            }
        }
    
        /* end 'enter' transition */
        &.ng-enter-active {
            .side-form {
                .content {
                    /* transition the right position which 
                       slides the content into view */
                    transition: right .5s;
    
                    /* end with content aligned to the right of the page */
                    right: 0;
                }
    
                .background {
                    /* transition the background opacity to fades it in */
                    transition: opacity .5s;
    
                    /* end with background opacity 0.8 to give the overlay effect */
                    opacity: .8;
                }
            }
        }
    
        /* end 'leave' transition */
        &.ng-leave-active {
            .side-form {
                .content {
                    /* transition the right position which 
                       slides the content out of view */
                    transition: right .5s;
    
                    /* end transition with the content completely off the page */
                    right: -100%;
                }
    
                .background {
                    /* transition the background opacity to fades it out */
                    transition: opacity .5s;
    
                    /* end with background opacity 0 to hide it */
                    opacity: 0;
                }
            }
        }
    }