本文介绍了如何在AngularJS中获取offsetHeight,scrollHeight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个需要三个值的指令-
I am creating a directive in which I need three values-
- scrollTop
- offsetHeight
-
scrollHeight
- scrollTop
- offsetHeight
scrollHeight
projectModule.directive('scroller', function ($window) {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
var rawElement = angular.element($window);
angular.element($window).bind('scroll', function () {
var scrollTop = rawElement.scrollTop();
var offsetHeight = rawElement.offsetHeight;
var scrollHeight = rawElement.scrollHeight;
console.log(scrollTop + " ," + offsetHeight + " ," + scrollHeight);
scope.$apply();
});
};
});
我可以使用rawElement.scrollTop()函数直接获取scrollTop,但是如何获取该元素的offsetHeight和scrollHeight?
I can get scrollTop directly by using rawElement.scrollTop() function, but How to get offsetHeight and scrollHeight of that element?
我想运用这种逻辑-
if ((rawElement.scrollTop + rawElement.offsetHeight + 5) >= rawElement.scrollHeight) {
scope.$apply(); //For infinite scrolling
}
谢谢.
推荐答案
我会尝试使用 $ document
服务,因为您实际上需要 document.body.scrollHeight
和 document.body.offsetHeight
.所以
I would try to make use of $document
service instead, since you actually need document.body.scrollHeight
and document.body.offsetHeight
. So
var offsetHeight = $document[0].body.offsetHeight;
var scrollHeight = $document[0].body.scrollHeight;
这篇关于如何在AngularJS中获取offsetHeight,scrollHeight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!