1

I'm trying to fix a div when scrolling down; for that, I began with the below code, I am facing some weird issue that prevents me from detecting the scrolling behavior. On the below code console.log('bigger than 50') does not get printed when scrolling down.

Any ideas why I can't detect the scroll event?

@Component({
templateUrl: 'outlet.html',
selector: "app-layout-nav"
})


export class place {


  constructor(@Inject(DOCUMENT) private document: Document) {}

  @HostListener("window:scroll", [])

  onWindowScroll() {
    let number = this.document.body.scrollTop;
    if (number > 50) {
    console.log('bigger than 50')
    this.navIsFixed = true;
   } else if (this.navIsFixed && number < 10) {
      console.log('lower than 50')
      this.navIsFixed = false;
    }
  }

}

Outlet.html

 <div>

 <div id="wrap" [class.fixed]="navIsFixed">
  This should be fixed when it reaches a certain level</div>

  <div>The rest of the long page should show here</div>

 </div>
7
  • 1
    You need to listen on the div that scrolls, not on window. window:scroll is only for when the whole page is scrolled. We would need to see the HTML to and a pointer to the element that is supposed to scroll. Commented Apr 22, 2017 at 13:49
  • @GünterZöchbauer, I've posted the rest of code. Basically, I believe that it should be window:scroll as the user should scroll the window, and a particular div on that window will be fixed after reaching a certain level of scrolling. Please correct me if I am wrong. Commented Apr 22, 2017 at 13:58
  • I can't know if you're wrong :D. If you say this is what should happen, then this is probably true. If the page is scrolling, then your @HostListener() should capture the scroll events, if it doesn't then not. Commented Apr 22, 2017 at 13:59
  • The problem is that @HostListener() does not capture the scroll events Commented Apr 22, 2017 at 14:01
  • I don't see a way to diagnose it from the code you posted. Can you create Plunker that reproduces the problem? Commented Apr 22, 2017 at 14:02

2 Answers 2

3

this might help you to detect scroll events on an element or on window https://github.com/anasAsh/ngx-scroll-event

Install

  • With NPM npm install ngx-scroll-event --save

  • With Yarn yarn add ngx-scroll-event

Usage

Import ScrollEvent and add it to the imports array of your module.

// app.module.ts
import { ScrollEventModule } from 'ngx-scroll-event';

@NgModule({
  imports: [
    ...,
    ScrollEventModule,
    ...,
  ]
})
export class AppModule { }

In your template you may now add the detect-scroll attribute and (onScroll) event to any element. you can also add [bottomOffest] to change when reaching bottom is alert is true, defaults to 100, the value should be a number in pixels.

// app.awesome.component.ts

...
import { ScrollEvent } from 'ngx-scroll-event';
...

@Component({
   ...
   template: `...
        <div detect-scroll (onScroll)="handleScroll($event)" [bottomOffest]="200">
            <div>Bla bla bla</div>
            <div>Bla bla bla</div>
            <div>Bla bla bla</div>
            <div>Bla bla bla</div>
            <div>Bla bla bla</div>
            <div>Bla bla bla</div>
            <div>Bla bla bla</div>
            <div>Bla bla bla</div>
            <div>Bla bla bla</div>
        <div>
   ...`,
})
export class AwesomeComponent {
  public handleScroll(event: ScrollEvent) {
    console.log('scroll occurred', event.originalEvent);
    if (event.isReachingBottom) {
      console.log(`the user is reaching the bottom`);
    }
    if (event.isWindowEvent) {
      console.log(`This event is hapening on Window not on an element.`);
    }

  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can bind the scroll event to your container:

<div class="container" (scroll)="onScroll($event)">
  <div class="scrollable_content">
  </div>
</div>

Then, on your component:

onScroll(event){
  const scrollTop = event.path[0].scrollTop;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.