2

I am creating a directive in which I need three values-

  1. scrollTop
  2. offsetHeight
  3. 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.

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.