I'm trying to create an affix
element om my page and I'm using pure AngularJs, no jquery.
I tried some different solutions to achieve this, but none of them seem to work. The problem is to get the offsetTop of the element i want to fix after the scroll.
This is running inside a directive that is executed on every scroll on the page.
This is the code i tried:
.directive("scroll", ['$window', function ($window) {
return function(scope, element, attrs) {
angular.element($window).bind("scroll", function() {
//This code returns: '0'
var filter= document.getElementById('filter').offsetTop;
//This code returns: Error: [jqLite:nosel]
var filter = angular.element('#filter').prop('offsetTop');
//This code returns: [object HTMLDivElement]
var filter = angular.element(document.querySelector('.filter')).prop('offsetTop');
//This code returns: '0'
var filter = angular.element(document.getElementsByClassName('filter')).prop('offsetTop');
[.. more code ..]
})
}
}]);
The problem is, none of those codes are working.
Or it has an undefined
value, or it returns a value of 0
, even when this element is on the pageYoffset
with a value of 700.
How can i get the offsetTop value of this element using pure AngularJs
or vanilla Javascript? I just don't to load Jquery in the page to just use in this function.