I'm trying to trigger an event when scroll bar reaches the end. I found this this example. Here is my code. The problem is that it doesn't call loadmore() at all. The values of the console statments are:
848
899
in scroll
881
899
in scroll
892
899
in scroll
897
899
in scroll
900
899
It seems that it never goes to that if statement! The weird part is that if I debug it in inspect element then it triggers the event. ..... my directive:
directive('scrolly', function () {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var raw = element[0];
console.log('loading directive');
element.bind('scroll', function () {
console.log('in scroll');
console.log(raw.scrollTop + raw.offsetHeight);
console.log(raw.scrollHeight);
if (raw.scrollTop + raw.offsetHeight == raw.scrollHeight) { //at the bottom
scope.$apply(attrs.scrolly);
}
})
}
}
I was looking for a snippet to help me do this, but couldn't find anything worthwhile so I came up with this fairly easy to use directive to do this
https://gist.github.com/podrezo/f80f35d6d0655f4d550cac4747c110ff
Here's a jsfiddle to try it out:
https://jsfiddle.net/va4x5r26/2/
Works with Angular 1.3 and 1.4.8, and tested on IE10 and Chrome 55.
Instead of checking for equality, please check if the left side is greater than the right side in the if statement since that's the case for the scrollbar to be at the bottom.
Here is the working jsfiddle.
I took the Mark's code and I made some changes to add support on div elements. Note that you can define another attribute to configure the threshold (50 in my case).
You gave me a few good tips ... heres a complete working example for anyone else who stumbles across this post:
JS:
HTML:
App Directive:
When browser's scroll bar reached to down, we need to fire an event. Below code will work for Angular 6.