CSS动画在动画期间向下移动第二个元素

时间:2015-10-07 06:52:48

标签: html css css-animations ng-animate animate.css

使用ng-if动画ngAnimate时出现问题。我有两个不同的容器互相替换,通过fadeInLeft新元素和fadeOutRight旧元素。但是,在动画期间,第二个容器向下移动。

以下是我的问题的简化示例(以全屏模式显示以查看确切问题):



var app = angular.module("MyApp", ['ngAnimate']);

app.controller("MyController", function($scope, $interval) {
  $scope.show = true;
  
  $interval(function() {
    $scope.show = !$scope.show;
    }, 3000);
  
  
});

.box {
  height: 200px;
}

.box.ng-enter {
  animation: fadeInLeft 1.0s;
}

.box.ng-leave {
  animation: fadeOutRight 1.0s;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-animate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.4.0/animate.css" />

<div ng-app="MyApp" ng-controller="MyController">
  <div ng-if="show" class="box" style="background:red"></div>
  <div ng-if="!show" class="box" style="background:blue"></div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

这是<div>块级元素占用自己空间的默认行为。您可以将position: absolute添加到框类中,以使其来自正常文档流。

同时添加width: 100%,因为绝对定位的div的宽度仅限于内容。

为了限制此区域内的定位,请确保将position: relative添加到父元素。

Updated Plunker

var app = angular.module("MyApp", ['ngAnimate']);

app.controller("MyController", function($scope, $interval) {
  $scope.show = true;

  $interval(function() {
    $scope.show = !$scope.show;
  }, 3000);


});
.box {
  height: 200px;
  position: absolute;
  width: 100%;
}
.box.ng-enter {
  animation: fadeInLeft 1.0s;
}
.box.ng-leave {
  animation: fadeOutRight 1.0s;
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular-animate.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.4.0/animate.css" />

<div ng-app="MyApp" ng-controller="MyController">
  <div ng-if="show" class="box" style="background:red"></div>
  <div ng-if="!show" class="box" style="background:blue"></div>
</div>