moh*_*med 7 scroll angularjs angularjs-ng-repeat
我尝试从解决方案到此工作
如何在AngularJS中保留ng-repeat的滚动位置?
在删除ng-repeat中的顶部项目时实现保留滚动位置但无法找出执行此操作的代码.
另外,请注意,列表应按与items数组相同的顺序打印,而不是像示例那样反向打印.
解决方案的代码:
angular.module("Demo", [])
.controller("DemoCtrl", function($scope) {
$scope.items = [];
for (var i = 0; i < 10; i++) {
$scope.items[i] = {
id: i,
name: 'item ' + i
};
}
$scope.addNewItem = function() {
$scope.items = $scope.items.concat({
id: $scope.items.length,
name: "item " + $scope.items.length
});
};
})
.directive("keepScroll", function(){
return {
controller : function($scope){
var element = 0;
this.setElement = function(el){
element = el;
}
this.addItem = function(item){
console.log("Adding item", item, item.clientHeight);
element.scrollTop = (element.scrollTop+item.clientHeight+1); //1px for margin
};
},
link : function(scope,el,attr, ctrl) {
ctrl.setElement(el[0]);
}
};
})
.directive("scrollItem", function(){
return{
require : "^keepScroll",
link : function(scope, el, att, scrCtrl){
scrCtrl.addItem(el[0]);
}
}
})
Run Code Online (Sandbox Code Playgroud)
我尝试做的是改变
element.scrollTop = (element.scrollTop + item.clientHeight+1)
Run Code Online (Sandbox Code Playgroud)
至
element.scrollTop = (element.scrollTop - item.clientHeight+1)
Run Code Online (Sandbox Code Playgroud)
并按'id'而不是'-id'按顺序打印
我认为最初的解决方案有点像hacky ......但这是一个使用它作为基础的工作编辑.
问题是解决方案取决于添加到ng-repeat的项目.如果查看scrollItem指令,它只会导致keepScroll指令在链接器执行时重新调整scrollTop.这仅在项目添加而不是删除时发生.
而编辑则侦听scope.$on('$destroy')事件.但是,此时的问题是该元素不再具有clientHeight,因为它已从DOM中删除.因此解决方案是在创建时保存其高度,然后告诉keepScroll删除元素的高度是多少.
注意:如果滚动条一直到底部,这似乎会导致滚动跳转,因此您需要将该情况视为异常.
工作JSBin:http://jsbin.com/geyapugezu/1/edit?html,css,js,output
以供参考:
HTML
<!DOCTYPE html>
<html>
<head>
<script src="//code.angularjs.org/1.3.0-beta.7/angular.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-app="Demo" ng-controller="DemoCtrl">
<div class="wrapper" keep-scroll>
<div class="item" scroll-item ng-repeat="item in items | orderBy: 'id'">
{{ item.name }}
</div>
</div>
<button ng-click="removeItem()">
Remove item
</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
CSS
.wrapper {
width: 200px;
height: 300px;
border: 1px solid black;
overflow: auto;
}
.item {
background-color: #ccc;
height: 100px;
margin-bottom: 1px;
}
Run Code Online (Sandbox Code Playgroud)
JS
angular.module("Demo", [])
.controller("DemoCtrl", function($scope) {
$scope.items = [];
for (var i = 0; i < 10; i++) {
$scope.items[i] = {
id: i,
name: 'item ' + i
};
}
$scope.removeItem = function() {
$scope.items = $scope.items.slice(1);
};
})
.directive("keepScroll", function(){
return {
controller : function($scope){
var element = 0;
this.setElement = function(el){
element = el;
};
this.itemRemoved = function(height){
element.scrollTop = (element.scrollTop - height - 1); //1px for margin
console.log("Item removed", element.scrollTop);
};
},
link : function(scope,el,attr, ctrl) {
ctrl.setElement(el[0]);
}
};
})
.directive("scrollItem", function(){
return {
require : "^keepScroll",
link : function(scope, el, att, scrCtrl){
var height = el[0].clientHeight;
scope.$on('$destroy', function() {
scrCtrl.itemRemoved(height);
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
或者,这样做.不需要scrollItem,而是我们观察ng-repeat项目的更改并相应地重新调整scrollTop.
JSBin:http://jsbin.com/dibeqivubi/edit?html,css,js,output
JS
angular.module("Demo", [])
.controller("DemoCtrl", ['$scope', function($scope) {
$scope.items = [];
for (var i = 0; i < 10; i++) {
$scope.items[i] = {
id: i,
name: 'item ' + i
};
}
$scope.removeItem = function() {
$scope.items = $scope.items.slice(1);
};
}])
.directive("keepScroll", function() {
return {
link : function(scope,el,attr, ctrl) {
var scrollHeight;
scope.$watchCollection('items', function(n,o) {
// Instantiate scrollHeight when the list is
// done loading.
scrollHeight = scrollHeight || el[0].scrollHeight;
// Adjust scrollTop if scrollHeight has changed (items
// have been removed)
el[0].scrollTop = el[0].scrollTop - (scrollHeight - el[0].scrollHeight);
// Remember current scrollHeight for next change.
scrollHeight = el[0].scrollHeight;
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
HTML
<!DOCTYPE html>
<html>
<head>
<script src="//code.angularjs.org/1.3.0-beta.7/angular.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-app="Demo" ng-controller="DemoCtrl">
<div class="wrapper" keep-scroll>
<div class="item" ng-repeat="item in items | orderBy: 'id'">
{{ item.name }}
</div>
</div>
<button ng-click="removeItem()">
Remove item
</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4322 次 |
| 最近记录: |