question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add scroll events to Angular.

See original GitHub issue

[ ] bug report [x] feature request [ ] support request

Current behavior

I’ve done a research to find a functionality within Angular 2 framework to detect the vertical position of the scroll bar so I can trigger animations in certain breakpoints, but the only helpful solution was a work around using Renderer class. here is the code (toggle ‘shrink’ variable to active after 75px scrolled):

   constructor(private renderer: Renderer) {
    this.renderer.listenGlobal('window', 'scroll', (event) => { 
       this.shrink = (event.pageY>75 ? 'active' : 'inactive');
        });

the problem is that there is no built in functions for scroll events, moreover, the code above does the trick only on firefox web browser because the pageY attribute (contains the vertical position of the scroll bar in pixels) has a different name in the scroll event object given by the browsers other than firefox (event.pageY is undefined [Chrome/Edge]).

Expected behavior

Provide functionalities dedicated for scroll events that works with the majority of web browsers.

What is the motivation / use case for changing the behavior?

Scroll events are very useful especially when it comes to animations (sticky headers…).

My environment: Operating system: Windows 10 IDE: VS Code package manager: npm/yarn HTTP server: lite-server

  • Angular version: 2.2.0

  • Language: [all | TypeScript 2.0.3 | ES6/7 | ES5]

  • Node (for AoT issues): node --version = 7.1

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

18reactions
zoechicommented, Jan 22, 2017

@BigFax (click) listens only to click events of the element where you added it. (scroll) also listens only to the element where you add it. If you add (scroll) to a <div (scroll)="myScrollHandler($event)" it won’t be called when the window scrolls, only when the content of the div is scrolles. Perhaps you want <div (window:scroll)="myScrollHandler($event)"

7reactions
BigFaxcommented, Jan 22, 2017

I mean : i can do (click)="myClickFunc()" but i can’t do (scroll)="myScrollFunc()" click is supported, not scroll. I need to do :

@HostListener('window:scroll', ['$event']) 
doSomething(event) {
   // ...
}

From : https://angular.io/docs/ts/latest/guide/user-input.html#!#binding-to-user-input-events

You can use Angular event bindings to respond to any DOM event.

List of DOM event : https://developer.mozilla.org/en-US/docs/Web/Events But they are not all “supported” as “click” is by default. Is there a specifc list of DOM event supported by Angular ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get on scroll events? - Stack Overflow
1. remember to add import: import { HostListener } from '@angular/core'; · <div (window:scroll)="onWindowScroll($event)"> , clear, easy and works ...
Read more >
4 Ways to Listen to Page Scrolling for Dynamic UI in Angular ft ...
Even easier, you can window scroll from any HTML element in the DOM, using the window:scroll event emitter. With this method, you just...
Read more >
Scrolling - Angular Material
Sends an event when the directive's element is scrolled. Registers itself with the ScrollDispatcher service to include itself as part of its collection...
Read more >
Angular 2 Window Scroll Event Using Hostlistener - StackBlitz
Angular scroll event using @HostListener decorator.
Read more >
ngx-scroll-event - npm
ngx-scroll-event An Angular 2+ directive to emit an event if scroll occurs on an element or window.. Latest version: 2.0.2, last published: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found