AngularJS Get Element Scroll Position
AngularJS Get Element Scroll Position : To get the scroll position of an element in AngularJS, you can use the built-in $window
service and the $document
service. First, inject the $window
and $document
services into your controller or directive.
Then, use the $window
service to access the pageYOffset
property, which represents the vertical scroll position of the window. To get the scroll position of a specific element, use the $document
service to retrieve the element by its ID or class and access its scrollTop
property. This will give you the scroll position of the element.




Thanks for your feedback!
Your contributions will help us to improve service.
What is the method to retrieve the scroll position of an element in AngularJS?
This code snippet demonstrates how to track the scroll position of an element using AngularJS. It starts by defining an AngularJS module called "myApp" and a controller called "myController". The controller initializes two variables, $scope.positionTop and $scope.positionLeft, which represent the scroll position of the element.
The function $scope.trackElementScrollPosition is defined to track the scroll event of the element with the given ID. It uses the angular.element method to select the element and attaches a scroll event listener. When the scroll event occurs, the function updates the scroll positions and triggers an update of the AngularJS scope using $scope.$apply.
AngularJS Get Element Scroll Position
<div ng-app="myApp" ng-controller="myController" class="container">
<div id="myElement" ng-init="trackElementScrollPosition('myElement')">
<p>
Font Awesome is a popular icon set used by designers and developers
worldwide. It was created by Dave Gandy in 2012 and has since become
one of the most widely used icon sets on the web.
<br />
Font Awesome icons are vector-based, which means they can be scaled to
any size without losing their quality or becoming pixelated.
</p>
</div>
<p>Scroll Top: {{ positionTop }}px</p>
<p>Scroll left: {{ positionLeft }}px</p>
</div>
<script>
angular.module("myApp", []).controller("myController", function ($scope) {
$scope.positionTop = 0;
$scope.positionLeft = 0;
$scope.trackElementScrollPosition = function (elementId) {
var element = angular.element(document.getElementById(elementId));
element.on("scroll", function () {
$scope.$apply(function () {
$scope.positionTop = element[0].scrollTop;
$scope.positionLeft = element[0].scrollLeft;
});
});
};
});
</script>