【问题标题】:CSS animation moves second element down during animationCSS动画在动画期间向下移动第二个元素
【发布时间】:2015-10-07 06:52:47
【问题描述】:

我在使用ngAnimateng-if 制作动画时遇到问题。我有两个不同的容器,它们相互替换,通过新元素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>

【问题讨论】:

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


    【解决方案1】:

    这是&lt;div&gt;块级元素占用自己空间的默认行为。您可以将position: absolute 添加到 box 类中,使其脱离正常的文档流。

    还要添加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>

    【讨论】:

    • 感谢您的回答!但是使用您的解决方案时,当我将内容添加到动画div 并且未显示fadeOutRight 动画时,我仍然遇到问题。 plunkr
    • 我们可以在没有绝对位置的情况下这样做吗,这会让事情变得毫无意义
    猜你喜欢
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多