I am creating a directive in which I need three values-
- 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(); }); }; });
I can get scrollTop directly by using rawElement.scrollTop() function, but How to get offsetHeight and scrollHeight of that element?
I want to apply that logic-
if ((rawElement.scrollTop + rawElement.offsetHeight + 5) >= rawElement.scrollHeight) {
scope.$apply(); //For infinite scrolling
}
Thanks in advance.
I am creating a directive in which I need three values-
- 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(); }); }; });
I can get scrollTop directly by using rawElement.scrollTop() function, but How to get offsetHeight and scrollHeight of that element?
I want to apply that logic-
if ((rawElement.scrollTop + rawElement.offsetHeight + 5) >= rawElement.scrollHeight) {
scope.$apply(); //For infinite scrolling
}
Thanks in advance.
Share Improve this question edited Oct 15, 2014 at 7:01 Abhinav asked Oct 15, 2014 at 6:24 AbhinavAbhinav 3531 gold badge7 silver badges19 bronze badges2 Answers
Reset to default 3I 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;
Can you use getBoundingClientRect if $el is the actual DOM element?
var top = $el.getBoundingClientRect().top;
JSFiddle
You could also try using
$el.prop('offsetTop')