lps*_*wan 5 html javascript css angularjs
我很有棱角.我想使用ng-click元素在ng-repeat元素上设置div主体的动画.这是我到目前为止所尝试的.
app.js
var app = angular.module( 'app', [] );
app.controller('appController', function($scope) {
$scope.items = [
{"id": "id1", "name": "Name 1"},
{"id": "id2", "name": "Name 2"},
{"id": "id3", "name": "Name 3"}
];
$scope.selectedStyle = {"background-color": "blue", "color": "white"};
$scope.selectedItem = $scope.items[0];
$scope.selectItem = function(item) {
$scope.selectedItem = item;
}
});
Run Code Online (Sandbox Code Playgroud)
app.html
<div ng-app="app" ng-controller="appController">
<table class=table>
<tbody>
<tr ng-repeat="item in items" ng-click="selectItem(item)" ng-style="item.id === selectedItem.id && selectedStyle">
<td>
{{item.id}}
</td>
</tr>
</tbody>
</table>
<div class="item-body">
{{selectedItem.name}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我想要做的是将item-body div的淡入过渡效果添加为更改项.我在网上搜索,但似乎无法找到解决方案.请帮忙.
您可以通过使用angular向所选元素添加类,并使用css过渡管理过渡来完成此操作.
这样就没有必要了$scope.selectedStyle
.我们将在css中管理它.
selected
单击的元素添加一个类.item
将处理颜色的变化对你(无论是选择和取消).var app = angular.module('app', []);
app.controller('appController', function($scope) {
$scope.items = [{
"id": "id1",
"name": "Name 1"
}, {
"id": "id2",
"name": "Name 2"
}, {
"id": "id3",
"name": "Name 3"
}];
$scope.selectedItem = $scope.items[0];
$scope.selectItem = function(item) {
$scope.selectedItem = item;
}
});
Run Code Online (Sandbox Code Playgroud)
.item-body {
color: red;
}
.item {
cursor: pointer;
transition: all 250ms linear;
}
.item.selected {
cursor: default;
background-color: blue;
color: white;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="appController">
<table class=table>
<tbody>
<tr ng-repeat="item in items" ng-click="selectItem(item)" class="item" ng-class="{ 'selected': selectedItem === item }">
<td>
{{item.id}}
</td>
</tr>
</tbody>
</table>
<div class="item-body">
{{selectedItem.name}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
item-body
如果要为item-body
on更改设置动画,可以使用简单的超时来添加和删除类.
此外,您应该知道有一些模块可以用来实现这一点(像这样).
这是我的建议:
item-body
元素知道它需要隐藏和显示transition
我们之前所做的var app = angular.module('app', []);
app.controller('appController', function($scope, $timeout) {
$scope.items = [{
"id": "id1",
"name": "Name 1"
}, {
"id": "id2",
"name": "Name 2"
}, {
"id": "id3",
"name": "Name 3"
}];
$scope.selectedItem = $scope.items[0];
$scope.selectItem = function(item) {
$scope.changeIsOn = true;
$timeout(function() {
$scope.selectedItem = item;
$scope.changeIsOn = false;
}, 250);
}
});
Run Code Online (Sandbox Code Playgroud)
.item-body {
color: red;
transition: opacity 250ms linear;
}
.item-body.changing {
opacity: 0;
}
.item {
cursor: pointer;
transition: all 250ms linear;
}
.item.selected {
cursor: default;
background-color: blue;
color: white;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="appController">
<table class=table>
<tbody>
<tr ng-repeat="item in items" ng-click="selectItem(item)" class="item" ng-class="{ 'selected': selectedItem === item }">
<td>
{{item.id}}
</td>
</tr>
</tbody>
</table>
<div class="item-body" ng-class="{ 'changing': changeIsOn }">
{{selectedItem.name}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
575 次 |
最近记录: |