-1

I was just quite happy with this solution but i need one more thing. I want to give the link an "active" class when it jumps to the section, and remove it when i click to jump to another section and so on. Does anyone have an idea? Thanks

Sara Alba
  • 1
  • 4

1 Answers1

0

We can achieve this by using scrollspy. I've created the custom scrollspy angular directive. Here is the working example stackblitz Example

app.component.html

<h1>Current Section : [{{ currentSection }}]</h1>
    
    <h2>Menu</h2>
    <div
      (click)="scrollTo('section1')"
      [ngClass]="{ 'current-section': currentSection === 'section1' }"
    >
      Section 1
    </div>
    <div
      (click)="scrollTo('section2')"
      [ngClass]="{ 'current-section': currentSection === 'section2' }"
    >
      Section 2
    </div>
    <div
      (click)="scrollTo('section3')"
      [ngClass]="{ 'current-section': currentSection === 'section3' }"
    >
      Section 3
    </div>
    <div
      (click)="scrollTo('section4')"
      [ngClass]="{ 'current-section': currentSection === 'section4' }"
    >
      Section 4
    </div>
    <br />
    
    <div
      id="parentDiv"
      scrollSpy
      [spiedTags]="['DIV']"
      (sectionChange)="onSectionChange($event)"
      style="height:150px;overflow-y: scroll;"
    >
      <div id="section1">
        <h2 style="margin:0">Section 1</h2>
        <lorem-ipsum></lorem-ipsum>
      </div>
      <div id="section2">
        <h1>Section 2</h1>
        <lorem-ipsum></lorem-ipsum>
      </div>
      <div id="section3">
        <h1>Section 3</h1>
        <lorem-ipsum></lorem-ipsum>
      </div>
      <div id="section4">
        <h1>Section 4</h1>
        <lorem-ipsum></lorem-ipsum>
      </div>
    </div>
    <br />

app.component.ts

import { Component, VERSION } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Angular ' + VERSION.major;
  currentSection = 'section1';

  onSectionChange(sectionId: string) {
    this.currentSection = sectionId;
  }

  scrollTo(section) {
    document.querySelector('#' + section).scrollIntoView();
  }
}

app.component.css

p {
  font-family: Lato;
}

.current-section {
  background-color: cornflowerblue;
}
.current-section::after {
  content: '\039e';
  background-color: yellow;
  color: red;
  font-weight: bold;
  font-size: 1.5rem;
  margin-left: 0.5rem;
}

scroll-spy.directive.ts

import {
  Directive,
  Injectable,
  Input,
  EventEmitter,
  Output,
  ElementRef,
  HostListener,
} from '@angular/core';

@Directive({
  selector: '[scrollSpy]',
})
export class ScrollSpyDirective {
  @Input() public spiedTags = [];
  @Output() public sectionChange = new EventEmitter<string>();
  private currentSection: string;

  constructor(private _el: ElementRef) {}

  @HostListener('scroll', ['$event'])
  onScroll(event: any) {
    let currentSection: string;
    const children = this._el.nativeElement.children;
    const scrollTop = event.target.scrollTop;
    const parentOffset = event.target.offsetTop;
    for (let i = 0; i < children.length; i++) {
      const element = children[i];
      if (this.spiedTags.some((spiedTag) => spiedTag === element.tagName)) {
        if (element.offsetTop - parentOffset <= scrollTop) {
          currentSection = element.id;
        }
      }
    }
    if (currentSection !== this.currentSection) {
      this.currentSection = currentSection;
      this.sectionChange.emit(this.currentSection);
    }
  }
}

lorem-ipsum.component.ts

import { Component } from '@angular/core';


@Component({
  selector: 'lorem-ipsum',
  template: `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam nibh. Mauris ac mauris sed pede pellentesque fermentum. Maecenas adipiscing ante non diam sodales hendrerit.

Ut velit mauris, egestas sed, gravida nec, ornare ut, mi. Aenean ut orci vel massa suscipit pulvinar. Nulla sollicitudin. Fusce varius, ligula non tempus aliquam, nunc turpis ullamcorper nibh, in tempus sapien eros vitae ligula. Pellentesque rhoncus nunc et augue. Integer id felis. Curabitur aliquet pellentesque diam. Integer quis metus vitae elit lobortis egestas. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi vel erat non mauris convallis vehicula. Nulla et sapien. Integer tortor tellus, aliquam faucibus, convallis id, congue eu, quam. Mauris ullamcorper felis vitae erat. Proin feugiat, augue non elementum posuere, metus purus iaculis lectus, et tristique ligula justo vitae magna.`
})
export class LoremIpsumComponent {

}

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { LoremIpsumComponent } from './lorem-ipsum.component';
import { ScrollSpyDirective } from './scroll-spy.directive';

@NgModule({
  imports: [BrowserModule, FormsModule],
  declarations: [AppComponent, ScrollSpyDirective, LoremIpsumComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}
cfprabhu
  • 5,267
  • 2
  • 21
  • 30
  • thank you so much for the quick answer, the working example is great! In my app tho, i seem to have some problems, my navigation and sections are not in the same component and i have getting some errors. I have a header component where the navigation is, the home component where the sections are called as separate components as well. idk how to solve this – Sara Alba Aug 03 '22 at 07:37