Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS window scroll event does not fire

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:

  • Jquery .on('scroll') not firing the event while scrolling
  • or just the simple JQuery .scroll() event
  • window.onscroll = function(ev) ...

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?

like image 824
threxx Avatar asked May 03 '16 21:05

threxx


People also ask

Why can't I see the scroll event in the window?

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.

What are scroll events in JavaScript?

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

What is event throttling in OnScroll?

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.

How do I assign an event handler to the scroll event?

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...' ); });


2 Answers

I would just use

$(window).scroll(function () { alert('scrolled') })

OR you could use

angular.element($window).bind("scroll", function(e) {
    alert('scrolled')
})
like image 81
James Freund Avatar answered Sep 23 '22 15:09

James Freund


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!'); };

like image 31
Isaac Weingarten Avatar answered Sep 21 '22 15:09

Isaac Weingarten