I know there are some similar question concerning this problem, however I none of the solutions could help me.
I am using AngularJS and want to detect the scroll event. I tried plenty of versions on how to get the event, however at most it fires when first loaded then never again.
My last code I tried was the following:
$($window).on('scroll', alert('scrolled'));
But I also tried this:
and many more, but nothing works.
Can anyone tell my what I am doing wrong?
Update: I tried the directive stuff, this works perfectly fine with safari BUT however not with chrome. Whats going on?
Update2: It works with the chrome mobile view when using the shift key. Are there any restrictions on chrome to apple trackpad? WTF?
Probably your document isn't scrolling, but a div inside it is. The scroll event only bubbles up to the window if it's called from document. Also if you capture the event from document and call something like stopPropagation, you will not receive the event in window.
Introduction to the JavaScript scroll events When you scroll a document or an element, the scroll events fire. You can trigger the scroll events in the following ways, for example: Using the scrollbar manually
Then, execute the scroll event handler using the setInterval () every 300 milliseconds if the scroll events have been fired. This way of handling the scroll event is called the event throttling that throttles an onscroll ‘s underlying operation every 300 milliseconds. The throttling slows down the rate of execution of the scroll event handler.
or assign an event handler to the onscroll property of the target element: Typically, you handle the scroll events on the window object to handle the scroll of the whole webpage. The following shows how to attach an event handler to the scroll event of a page: window .addEventListener ( 'scroll' , (event) => { console .log ( 'Scrolling...' ); });
I would just use
$(window).scroll(function () { alert('scrolled') })
OR you could use
angular.element($window).bind("scroll", function(e) {
alert('scrolled')
})
there is no need for angular.element() inject $window to your controller, use the window.onscroll event
$window.onscroll = function (){
console.log('window was scrolled!');
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With