4

Just learning Angular again. Installed AngularCLI and am trying to add a class on scroll from what I had before using jquery. Do I need to use [ngClass] to check a variable with window location? I am trying to use @HostListener right now.

$(function () {
 $(document).scroll(function () {
   $nav.toggleClass('scrolled', $(this).scrollTop() > $nav.height());
 });
});

$(function() {
 $(document).scroll(function() {
  var x = $(this).scrollTop();
  if (x > 3300) {
    $nav.removeClass('scrolled');
  }
 });
});

4 Answers 4

20

You can do something like this-

import { Component, HostListener, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

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

  @HostListener('window:scroll', [])
  onWindowScroll() {
    if (document.body.scrollTop > 20 ||     
    document.documentElement.scrollTop > 20) {
      document.getElementById('subTitle').classList.add('red');
      document.getElementById('paragraph').classList.add('green');
    }
  }
  name = 'Angular';
}

See live example here: https://stackblitz.com/edit/angular-changeclassonscroll

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

Comments

8

The above solutions work, but I think it's cleaner and more appropriate to utilize the framework as much as possible.

Typescript file:

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

    scrolled: boolean = false;

    @HostListener("window:scroll", [])
    onWindowScroll() {
        this.scrolled = window.scrollY > 0;
    }
}

Html:

<div [ngClass]="{ className: scrolled }"></div>

1 Comment

this is the correct answer - the document.ElementById pure JS solution is anti pattern eventhough it works
0

You can use this code inside the class

@HostListener('window:scroll', [])
onWindowScroll(event: Event) {
    //set up the div "id=nav"
    if (document.body.scrollTop > 3300 ||
        document.documentElement.scrollTop > 3300) {
        document.getElementById('nav').classList.add('scrolled');
    }
    else {
        document.getElementById('nav').classList.remove('scrolled');
        this.innerWidth = 'auto';
    }
}

Comments

0

add import on your typeScript file on top

{import {Component,HostListener,Directive,HostBinding,Input} from '@angular/core';}

then in app.components.ts for general scroll

add

export class AppComponent {
  title = 'Tour of Heroes';
  scrolled: boolean = false;

  @HostListener("window:scroll", [])
  onWindowScroll() {
      this.scrolled = window.scrollY > 0;
  }
}

then add

[ngClass]="{ 'scroll-add' : scrolled }"

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.