Route-Dependent CSS Page Transitions in AngularJS

前端 未结 2 914
渐次进展
渐次进展 2020-12-15 00:55

I am new to AngularJS and would like to implement route dependent page transitions. For example, I would like the page to slide left, slide right or fade depending on the ro

2条回答
  •  借酒劲吻你
    2020-12-15 01:37

    I looked in your plunker. The problem is with the way you use classes to animate your views.

    When the $routeChangeSuccess event is fired, ngView had already removed the class before you get the chance of changing the direction. You override it by applying the new class so quickly so it would not be noticed but then you get the digest in progress error.

    My solution (plunker):

    I came up with a directive:

    app.directive('animClass',function($route){
      return {
        link: function(scope, elm, attrs){
          var enterClass = $route.current.animate;
          elm.addClass(enterClass);
          scope.$on('$destroy',function(){
            elm.removeClass(enterClass);
            elm.addClass($route.current.animate);
          })
        }
      }
    });
    

    Declare an animate option for each route:

    app.config(function($routeProvider) {
      $routeProvider.
        when("/page1", {
          templateUrl: "page1.html",
          controller: "Page1Ctrl",
          animate: "slideLeft"
        }).
        when("/page2", {
          templateUrl: "page2.html",
          controller: "Page2Ctrl",
          animate: "slideRight"
        }).
        otherwise({
          redirectTo: "/page1"
        });
    });
    

    And just add it to ngView like so:

    css:

    .view {
        width: 100%;
        padding-left: 1em;
        position:absolute;
        top: 0;
        left: 0;
    }
    
    .slideLeft.ng-enter, .slideLeft.ng-leave, .slideRight.ng-enter, .slideRight.ng-leave  {
        -webkit-transition:all 1s;
        transition:all 1s;
    }
    
    .slideLeft.ng-enter {
        left:100%;
    }
    
    .slideLeft.ng-enter.ng-enter-active {
        left:0;
    }
    
    .slideLeft.ng-leave.ng-leave-active {
        left:-100%;
    }
    
    .slideRight.ng-enter {
        left:-100%;
    }
    
    .slideRight.ng-enter.ng-enter-active {
        left:0;
    }
    
    .slideRight.ng-leave.ng-leave-active {
        left:100%;
    }
    

提交回复
热议问题