我是通过jquery创建的:http://infinitynewtab.com/test/2/index.html
我想知道如何通过angularjs:http://infinitynewtab.com/test/1/index.html
来实现这个目标我的意思是实现过渡动画。
HTML
<html ng-app="myapp">
<head>
<meta charset="UTF-8">
<title>angularJs Animation</title>
<script src="js/angular.min.js"></script>
<script src="js/angular-route.min.js"></script>
<script src="js/angular-animate.min.js"></script>
<script src="js/main.js"></script>
<style>
.div{
width:80px;
height:80px;
margin:50px;
float: left;
background-color:red;
border-radius:100%;
text-align:center;
font-size:30px;
line-height:80px;
color:#fff;
cursor: pointer;
}
</style>
</head>
<body ng-controller='mytest'>
<button ng-click="add()" style="position: absolute;">Click to Add</button>
<br>
<p>Click the red circle to remove it</p>
<div class="div" ng-repeat="t in test track by $index" ng-click="delete($index)" ng-animate="{enter: 'animate-enter', leave: 'animate-leave'}">
{{t}}
</div>
</body>
</html>
的Javascript
Array.prototype.remove = function(dx) {
if (isNaN(dx) || dx > this.length) {
return false;
}
for (var i = 0, n = 0; i < this.length; i++) {
if (this[i] != this[dx]) {
this[n++] = this[i]
}
}
this.length -= 1
}
var app = angular.module('myapp', ['ngRoute', 'ngAnimate']);
app.controller('mytest', function($scope) {
$scope.test = [
1, 2, 3, 4, 5
];
$scope.delete = function(index) {
$scope.test.remove(index);
}
$scope.add = function() {
var a = Math.round(Math.random() * 10);
$scope.test.push(a);
}
});
答案 0 :(得分:2)
自Angular v1.2起,您不在HTML(ng-animate
属性)中使用ngAnimate指令。相反,您在CSS中定义ng-leave
和ng-leave-active
类:
angular.module('myapp', ['ngAnimate'])
.controller('mytest', function($scope) {
$scope.test = [
{value: 1}, {value: 2}, {value: 3}, {value: 4}, {value: 5}
];
$scope.delete = function(index) {
$scope.test.splice(index, 1);
}
$scope.add = function() {
var a = Math.round(Math.random() * 10);
$scope.test.push({value: a});
}
});
&#13;
.div {
width: 80px;
height: 80px;
margin: 50px;
float: left;
background-color: red;
border-radius: 100%;
text-align: center;
font-size: 30px;
line-height: 80px;
color: #fff;
cursor: pointer;
}
.div.ng-enter,
.div.ng-leave {
-webkit-transition: all .5s linear;
transition: all .5s linear;
}
.div.ng-enter {
-webkit-transform: scale(0);
}
.div.ng-enter-active {
-webkit-transform: scale(1);
}
.div.ng-leave-active {
-webkit-transform: scale(0);
}
&#13;
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.3/angular.js" data-semver="1.4.3"></script>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.3/angular-animate.js" data-semver="1.4.3"></script>
<div ng-app="myapp" ng-controller="mytest">
<button ng-click="add()">Click to Add</button>
<p>Click the red circle to remove it</p>
<div class="div" ng-repeat="t in test" ng-click="delete($index)">
{{t.value}}
</div>
</div>
&#13;
另请注意,在这种情况下track by $index
存在问题,因此最好更改对象数组的数据结构,以便在项目删除时正确跟踪(请参阅{{3 }})。最后,我删除了删除数组方法,因为简单Array.prototype.splice
更加清晰和有效。