2

I have one page with sections and fixed menu. When user clicks on menu item, the page will scroll to corresponding section.

For scrolling I use https://github.com/Nolanus/ng2-page-scroll

How do I add 'active' class to menu item on click and also when user scrolls to new section?

EDIT: Progress -- add 'active' class on Click:
My html:

<ul class="nav nav-menu">
    <li class="" [class.active]="activeLink == 'item1'" (click)="setActiveLink('item1')">
        <a pageScroll href="#item1">item 1</a>
    </li>       
</ul>

And in my component:

private activeLink: string = 'default-active-link';

setActiveLink(link: string){
   this.activeLink = link;
}

But how to get it working also on scroll?

2
  • post your code? Commented Feb 21, 2017 at 16:42
  • Maybe RouterLinkActive is what you're looking for? Commented Feb 21, 2017 at 22:08

2 Answers 2

1

According to Offical Documentation

The RouterLinkActive directive lets you add a CSS class to an element when the link's route becomes active.

<a routerLink="/user/bob" routerLinkActive="active-link">Bob</a>
When the url is either '/user' or '/user/bob', the active-link class will be added to the a tag. If the url changes, the class will be removed.

You can set more than one class, as follows:

<a routerLink="/user/bob" routerLinkActive="class1 class2">Bob</a>
<a routerLink="/user/bob" [routerLinkActive]="['class1', 'class2']">Bob</a>

You can configure RouterLinkActive by passing exact: true. This will add the classes only when the url matches the link exactly.

<a routerLink="/user/bob" routerLinkActive="active-link" [routerLinkActiveOptions]="{exact:
true}">Bob</a>
Sign up to request clarification or add additional context in comments.

1 Comment

This has no use, because I don't change pages, everything happens on one page.
1

you can use the angular animation for Transitioning between twostates:

import {
      Component,
      Input,
      trigger,
      state,
      style,
      transition,
      animate
    } from '@angular/core';
    import { Heroes } from './hero.service';
    @Component({
      moduleId: module.id,
      selector: 'hero-list-basic',
      template: `
        <ul>
          <li *ngFor="let hero of heroes"
              [@heroState]="hero.state"
              (click)="hero.toggleState()">
            {{hero.name}}
          </li>
        </ul>
      `,
      styleUrls: ['./hero-list.component.css'],
      animations: [
        trigger('heroState', [
          state('inactive', style({
            backgroundColor: '#eee',
            transform: 'scale(1)'
          })),
          state('active',   style({
            backgroundColor: '#cfd8dc',
            transform: 'scale(1.1)'
          })),
          transition('inactive => active', animate('100ms ease-in')),
          transition('active => inactive', animate('100ms ease-out'))
        ])
      ]
    })
    export class HeroListBasicComponent {
      @Input() heroes: Heroes;
    }

can also refer to this link for more details:

https://angular.io/docs/ts/latest/guide/animations.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.