anilCSE
anilCSE

Reputation: 2461

How to know the position of the scrollbar in a div?

I want to get the actual position (scrollY) of the scrollbar something like below. http://plnkr.co/edit/45gKhAIt0DrozS7f0Bz2?p=preview
I need to get this type of scrollY result for my div rather than the entire window. How can I do that in angularjs?

angular.element($window).bind('scroll',function(){
    console.log(' --- You scrolled - do $digest() ---')
    $scope.scrollY = $window.scrollY;
    $scope.$digest();
})

Upvotes: 2

Views: 9721

Answers (1)

TheSharpieOne
TheSharpieOne

Reputation: 25726

Here is not angular, just standard JavaScript: .scrollTop

yourDiv.scrollTop

Example: http://jsfiddle.net/TheSharpieOne/Tf7bq/2/ (The value is logged in the console.)

UPDATE

Changed listener to listen for scroll on the div. Angular doesn't have a nice way to listen for scroll (like it does for certain other events), so a standard JavaScript event listener is used. You can wrap this in a directive to make it more angular.

New example: http://jsfiddle.net/TheSharpieOne/Tf7bq/4/

yourDiv.addEventListener('scroll',function(){

    console.log(this.scrollTop);

});

Upvotes: 9

Related Questions