Skip to content Skip to sidebar Skip to footer

Angularjs 'scrolltop' Equivalent?

I'm looking to implement something similar to this in an AngularJS directive: https://github.com/geniuscarrier/scrollToTop/blob/master/jquery.scrollToTop.js It's fairly straightfor

Solution 1:

$window.pageYOffset

This is property from service $window

Solution 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.

https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollTop

document.body.scrollTop

Fiddle for you: http://jsfiddle.net/cdwgsbq5/

Solution 3:

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

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

Fiddle

Adaptation from another stackoverflow answer

Solution 4:

You can use like as polyfill here a link

functionoffset(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 };
}

Solution 5:

You can use

angular.element(document).bind('scroll', function() {
    if (window.scrollTop() > 100) {
        $this.fadeIn();
    } else {
        $this.fadeOut();
    }
});

Post a Comment for "Angularjs 'scrolltop' Equivalent?"