javascript – 使用AngularJS中的ngInfiniteScroll指令实现反向无限滚动
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 使用AngularJS中的ngInfiniteScroll指令实现反向无限滚动,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2014字,纯文字阅读大概需要3分钟。
内容图文
![javascript – 使用AngularJS中的ngInfiniteScroll指令实现反向无限滚动](/upload/InfoBanner/zyjiaocheng/698/78dee253aa3d49179e16d4ec0600a86d.jpg)
我想在这里使用ngInfiniteScroll指令:http://binarymuse.github.io/ngInfiniteScroll/在我的angular js应用程序中实现反向无限滚动(就像在聊天小部件中一样).但是,该指令的文档似乎没有提到如何实现.它仅记录了如何实现标准无限滚动.有人可以在这方面指导我吗?谢谢!
P.S:我热衷于使用这个指令,因为它处理DOM控件;来自angular的标准无限滚动指令在滚动时不断创建DOM元素,永远不会删除.
解决方法:
我认为你应该采用基于模型的方法(特别适合角度)
即,当您向上滚动并达到限制时,您会加载更多数据并将其插入到消息集合的开头(如果您希望限制因性能原因而加载的html节点数量,也可以删除最新数据).
这是我在lrInfiniteScroll中使用的那种方法
根本没有dom操作,它只是检测你向下滚动到达底部(带有去抖动)并触发你绑定的处理程序,这样你就可以做任何你想做的事情,特别是改变你的模型:你的应用程序仍然是模型驱动的
我已经改变了逻辑以使滚动向上行为,但这个想法保持不变
(function (ng) {
'use strict';
var module = ng.module('lrInfiniteScroll', []);
module.directive('lrInfiniteScroll', ['$timeout', function (timeout) {
return{
link: function (scope, element, attr) {
var
lengthThreshold = attr.scrollThreshold || 50,
timeThreshold = attr.timeThreshold || 400,
handler = scope.$eval(attr.lrInfiniteScroll),
promise = null,
lastScrolled = -9999;
lengthThreshold = parseInt(lengthThreshold, 10);
timeThreshold = parseInt(timeThreshold, 10);
if (!handler || !ng.isFunction(handler)) {
handler = ng.noop;
}
element.bind('scroll', function () {
var scrolled = element[0].scrollTop;
//if we have reached the threshold and we scroll up
if (scrolled < lengthThreshold && (scrolled - lastScrolled) < 0) {
//if there is already a timer running which has no expired yet we have to cancel it and restart the timer
if (promise !== null) {
timeout.cancel(promise);
}
promise = timeout(function () {
handler();
//scroll a bit againg
element[0].scrollTop=element[0].clientHeight/2;
promise = null;
}, timeThreshold);
}
lastScrolled = scrolled;
});
//scroll first to the bottom (with a delay so the elements are rendered)
timeout(function(){
element[0].scrollTop=element[0].clientHeight;
},0);
}
};
}]);
})(angular);
内容总结
以上是互联网集市为您收集整理的javascript – 使用AngularJS中的ngInfiniteScroll指令实现反向无限滚动全部内容,希望文章能够帮你解决javascript – 使用AngularJS中的ngInfiniteScroll指令实现反向无限滚动所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。