2

我有以下 AngularJS (v1.1.4) 代码,并且在将 ng-include 添加到 DOM 时尝试淡入(动画)它。我究竟做错了什么?此外,如果有人可以提出一种将添加/设置/删除命令传递给指令而不是观看“动作”属性的更好方法,那将不胜感激。

Plunker 在这里:http ://plnkr.co/edit/rcgpI0n8fGWj6o01Mp3b?p=preview

索引.html

<!doctype html>
<html ng-app="plunker" >
<head>
  <meta charset="utf-8">
  <title>AngularJS Plunker</title>
  <link rel="stylesheet" href="styles.css" />
  <script>document.write('<base href="' + document.location + '" />');</script>
  <script src="http://code.angularjs.org/1.1.4/angular.js"></script>
  <script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">

  <button ng-click="loadPartial('partial1.html')">Click to load partial 1</button>
  <button ng-click="loadPartial('partial2.html')">Click to load partial 2</button>
  <button ng-click="loadPartial('partial3.html')">Click to load partial 3</button>

  <div views></div>

</body>
</html>

应用程序.js

var app = angular.module('plunker', []);

app.controller('MainCtrl', ['$scope', 'viewsAPI', function(scope, viewsAPI) {

  scope.loadPartial = function (name) {
    viewsAPI.addView(name);
  };

}]);


app.factory('viewsAPI', function () {

    return {
        views: [],
        action: null,
        addView: function (viewName, options) {
            var view = { name: viewName, options: options };
            this.action = { type: 'add', view: view };
        },
        setView: function (viewName, options) {
            var view = { name: viewName, options: options };
            this.action = { type: 'set', view: view };
        },
        removeView: function () {
            this.action = { type: 'remove' };
        }
    }
});


app.directive('views', ['$compile', function (compile) {

    return {
        restrict: 'A',
        scope: {},
        replace: true,
        template: '<div class="views"></div>',

        controller: ['$scope', 'viewsAPI', function (scope, viewsAPI) {

            scope.api = viewsAPI;

            scope.$watch('api.action', actionChange, true);

            function actionChange (action) {

                if (!action) return;

                if (action.type == 'add') {
                    var view = scope.addView(action.view);
                    scope.api.views.push(view);
                }
                else if (action.type == 'set') {
                    scope.setView(action.view);
                }
                else if (action.type == 'remove') {
                    scope.removeView();
                }
            }
        }],

        link: function (scope, elm) {

            scope.addView = function (view) {
                var v = compile('<div class="view-wrapper" ng-include="\'' + view.name + '\'" ng-animate="fade"></div>')(scope);
                elm.append(v);
                return v;
            };

            scope.setView = function (view) {
            };

            scope.removeView = function () {
            };
        }
    };
}]);

样式.css

.fade-enter-setup { -webkit-transition: all 3s linear; opacity: 0; }
.fade-enter-setup { opacity: 1; }

部分1.html

<div>Hello I am a partial 1</div>

部分2.html

<div>PARTIAL 2-------------------</div>

部分3.html

<div>
33333333333333333333333333
<br />
this is partial 3
<br />
33333333333333333333333333
</div>
4

2 回答 2

3

如果您仍然感兴趣,我今天不小心创建了一个 plunker,应该可以回答您很久以前的问题。

http://plnkr.co/Gd5f6J

现在使用最新的 1.2 稳定版本,如果 angular 找到 ngAnimate 模块,当检测到像 ng-if、ng-switch、ng-view 这样的内置指令时,它会将特定的 css 类添加到 dom 中,即孩子已更改。这些类会触发您的 css 转换,因此如果您的部分视图需要很长时间才能加载,则动画将在完全加载时开始。

索引.html:

<div class="view-animate-container">
  <div class="well slide" ng-view>
    <!-- view container. Asynchronously loaded view templates are placed here -->
  </div>
</div>

应用程序.js:

angular.module('myApp', ['ngRoute', 'ngAnimate'])
.run(function($rootScope, $location){
  $rootScope.matchesRoute = function() {
    for(var i=0; i<arguments.length; i++) {
      if($location.path() === arguments[i]) {
        return true;
      }
    }
    return false;
  }
})
.config(function($routeProvider) {
    $routeProvider
      .when('/bellsAndWhistles', {templateUrl: 'bellsAndWhistles.html', controller: angular.noop()})
      .when('/about',            {templateUrl: 'about.html',            controller: angular.noop()})
      .otherwise({redirectTo: '', templateUrl: 'home.html'});
});

样式.css:

html, body{
  height: 100%;
}

.view-animate-container {
    position: relative;
    overflow: hidden;
    height:100%;
}

.view-animate-container > .slide.ng-enter,
.view-animate-container > .slide.ng-leave {
    -webkit-transition: all ease 0.5s;
       -moz-transition: all ease 0.5s;
         -o-transition: all ease 0.5s;
            transition: all ease 0.5s;  
    width: 100%;
    position:absolute;
}

.view-animate-container > .slide.ng-enter {
    left:100%;
    opacity: 0;
}
.view-animate-container > .slide.ng-enter.ng-enter-active {
    left:0;
    opacity: 1;
}
.view-animate-container > .slide.ng-leave.ng-leave-active {
    left: -100%;
    opacity: 0;
}
于 2013-11-17T10:01:32.253 回答
2

我遇到了同样的问题。但是,动画视图下方的内容行为将是跳跃和不受欢迎的。请参阅此Plunker 示例

因此,为了扩展@angabriel 的答案,我研究了一个替代方案。请参阅以下Plunker 示例,这是对 @angabriel 工作的修改。你需要使用 Chrome 才能看到它的工作,因为我没有机会为其他浏览器添加 css。

主要区别在于CSS。值得一提的是,我必须覆盖以下内容:

  • min-height
  • margin-top
  • margin-bottom
  • padding-top
  • padding-bottom

为了使过渡平滑,因为引导程序添加了一些规则,一旦离开的元素从 DOM 中删除,就会使过渡变得跳跃。

/* CSS */
.view-animate-container > .slide.ng-enter,
.view-animate-container > .slide.ng-leave {
    -webkit-transition: all ease 0.5s;
    -moz-transition: all ease 0.5s;
    -o-transition: all ease 0.5s;
    transition: all ease 0.5s;
    width: 100%;
    position: relative;
}

.view-animate-container > .slide.ng-enter {
    -webkit-animation-name: slideIn;
    animation-name: slideIn;
    -webkit-animation-duration: .5s;
    animation-duration: .5s;
    -webkit-animation: slideIn .5s;
    animation: slideIn .5s;
}

.view-animate-container > .slide.ng-leave {
    -webkit-animation-name: slideOut;
    animation-name: slideOut;
    -webkit-animation-duration: .5s;
    animation-duration: .5s;
    -webkit-animation: slideOut .5s;
    animation: slideOut .5s;
}

@keyframes slideOut {
    from {
        opacity: 1;
        max-height: 500px;
    }
    50% {
        opacity: 0;
        max-height: 0px;
        margin-top: 0;
        min-height: 0;
        margin-bottom: 0;
        padding-bottom: 0;
        padding-top: 0;
    }
    to {
        opacity: 0;
        max-height: 0px;
        margin-top: 0;
        min-height: 0;
        margin-bottom: 0;
        padding-bottom: 0;
        padding-top: 0;
    }
}

@keyframes slideIn {
    from {
        opacity: 0;
        max-height: 0px;
        margin-top: 0;
        min-height: 0;
        margin-bottom: 0;
        padding-bottom: 0;
        padding-top: 0;
    }
    50% {
        max-height: 0;
        opacity: 0;
        margin-top: 0;
        min-height: 0;
        margin-bottom: 0;
        padding-bottom: 0;
        padding-top: 0;
    }
    to {
        max-height: 500px;
        opacity: 1;
    }
}

优点

视图 div 下方的内容不会跳动

缺点

内容高度:如果视图内容大于(在我的情况下)它将动画到 500px 然后跳转到它的全高(正如Isaac500px所指出的)

谢谢

于 2016-04-08T14:58:47.700 回答