AngularJS 'scrollTop' equivalent?

All we need is an easy explanation of the problem, so here it is.

I’m looking to implement something similar to this in an AngularJS directive:

It’s fairly straightforward, when you are not at the top of the page it will fade in a button to scroll to the top:

$(window).scroll(function() {
  if ($(this).scrollTop() > 100) {
  } else {

However I’m having a hard time finding how to get the current scroll location in Angular. I’d rather not have to use jQuery just for this single thing.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1


This is property from service $window

Method 2

I don’t believe there’s anything in Angular to get the scroll position. Just use plain vanilla JS.

You can retrieve the scrollTop property on any element.


Fiddle for you:

Method 3

Inject the $window into your controller and you can get the scroll position on scroll

var windowEl = angular.element($window);
var handler = function() {
windowEl.on('scroll', handler);


Adaptation from another stackoverflow answer

Method 4

You can use like as polyfill
here a link

function offset(elm) {
  try {return elm.offset();} catch(e) {}
  var rawDom = elm[0];
  var _x = 0;
  var _y = 0;
  var body = document.documentElement || document.body;
  var scrollX = window.pageXOffset || body.scrollLeft;
  var scrollY = window.pageYOffset || body.scrollTop;
  _x = rawDom.getBoundingClientRect().left + scrollX;
  _y = rawDom.getBoundingClientRect().top + scrollY;
  return { left: _x, top: _y };

Method 5

You can use

angular.element(document).bind('scroll', function() {
    if (window.scrollTop() > 100) {
    } else {

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply