在AngularsJs中逐渐淡出和淡出?

时间:2017-11-13 22:13:57

标签: html css angularjs

我是Angulars的新手,我正在使用1.6.5版本。

我希望我的元素在点击时不仅仅显示淡出,而且在点击时淡出而不是仅仅是隐藏,这是我的代码HTML:

<ul>
            <li class="try" ng-if="bool"><label>email</label> : taoufiq.benallah@gmail.com</li>
            <li ng-click="bool=false"><label>téléphone</label> : +2126-13-85-98-34 </li>
            <li ng-click="bool=true"><label>website</label> : oujda.ma.taoufiq.com</li>
</ul>

这是我的CSS文件:

.try.ng-enter {
transition: 2s all;
opacity: 1;
}

.try.ng-enter.ng-enter-active {
transition: 2s all;
opacity: 1;
 }

.try.ng-leave {
transition: 2s all;
opacity : 1;
 }
.try.ng-leave.ng-leave-active {
transition: 2s all;
opacity: 1;
 }

我遇到的问题是我的元素不会出现在2s但是立即出现.. 感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为您使用的是ng-if。这使您的元素完全从DOM中消失。将其更改为ng-show,它将解决您的问题。

完整代码:

<ul>
   <li class="try" ng-show="bool"><label>email</label> : taoufiq.benallah@gmail.com</li>
   <li ng-click="bool=false"><label>téléphone</label> : +2126-13-85-98-34 </li>
   <li ng-click="bool=true"><label>website</label> : oujda.ma.taoufiq.com</li>
</ul>

答案 1 :(得分:0)

你也可以使用AngA方式的ngAnimate:

&#13;
&#13;
var app = angular.module('taskApp', ['ngAnimate']);

app.controller('taskCtrl', function($scope) {
  $scope.tasks = [
    { title: 'Go to the Zoo'},
    { title: 'Take a Nap'},
    { title: 'Start Learning AngularJS'},
    { title: 'Meet Susan at the Gym'}
  ];

  $scope.addtask = function() {
    $scope.tasks.push($scope.task);
    $scope.task = {};
  };

  $scope.removeItem = function(index) {
    $scope.tasks.splice(index, 1);
  };
});
&#13;
body {
  font-family: 'Lato';
  margin: 10px auto;
  max-width: 800px;
}

ul {
  padding: 0;
  margin-top: 40px;
  margin-left: 10px;
  font-size: 1.1em;
  position: relative;
}

form {
  margin-left: 10px;
}

ul li {
  list-style: none;
  margin: 15px auto;
  position: relative;
}

h1 {
  margin: 10px;
  text-align: center;
}

h2 {
  margin: 10px;
}

p {
  margin-left: 10px;
}

form {
  margin-top: 50px;
}

input {
  border: none;
  border-bottom: 1px solid orange;
  padding: 4px 10px;
  outline: none;
}

button {
  border: none;
  background: cornflowerblue;
  color: white;
  padding: 5px 12px;
  margin-left: 20px;
  border-radius: 4px;
}

.done {
  font-size: 0.7em;
  font-weight: bold;
  background: black;
  color: white;
  padding: 1px 5px;
  position: relative;
  top: -3px;
  border-radius: 2px;
  margin-right: 10px;
  cursor: pointer;
}

.done:hover {
  background: rgb(200, 0, 0);
}

@keyframes added {
  from {
    opacity: 0;
    top: -500px;
  }
  to {
    opacity: 1;
    top: 0px;
  }
}

@keyframes deleted {
  from {
    top: 0;
    opacity: 1;
  }
  to {
    top: 200px;
    opacity: 0;
  }
}

.task-item.ng-enter {
  animation: 0.25s linear added;
}

.task-item.ng-leave {
  animation: 0.25s linear deleted;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-animate.js"></script>

<body ng-app="taskApp">
  <h2>A Random List of Tasks</h2>
  <div ng-controller="taskCtrl">
    <form ng-submit="addtask()">
      <input type="text" ng-model="task.title">
      <button type="submit">Add Task</button>
      <br>
    </form>
    <ul>
      <li class="task-item" ng-repeat="task in tasks">
        <span class="done" ng-click="removeItem($index)">X</span>
        <span>{{task.title}}</span>
      </li>
    </ul>
  </div>
</body>
&#13;
&#13;
&#13;