As I work on my single-page website using Angular 2 and Bootstrap 4, I have successfully implemented a fixed navbar component that remains at the top of the page. Utilizing anchor navigation (#id), the navbar allows smooth scrolling to different sections on the same page without needing a routing module.
The code for the navbar can be found below:
<nav class="navbar navbar-default fixed-top navbar-inverse navbar-toggleable-md">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggler navbar-toggler-right btn btn-outline-secondary" (click)="isCollapsed = !isCollapsed" [attr.aria-expanded]="!isCollapsed" aria-controls="collapseExample">
<i class="fa fa-bars" aria-hidden="true"></i>
</button>
<a class="navbar-brand" href="index.html">Garatéa</a>
</div>
<div id="collapseExample" class="navbar-collapse collapse" [ngbCollapse]="isCollapsed">
<ul class="nav navbar-nav ml-auto">
<li class="nav-item"><a class="nav-link" href="#home">Home</a></li>
<li class="nav-item"><a class="nav-link" href="#about">About</a></li>
<li class="nav-item"><a class="nav-link" href="#team">Team</a></li>
<li class="nav-item"><a class="nav-link" href="#contact">Contact</a></li>
</ul>
</div>
</div>
</nav>
Each link within the navbar corresponds to a section on the website, such as the 'home' section template shown below:
<div id="home" class="text-center home-background">
<div class="overlay">
<div class="content">
<h1>Welcome to <strong><span class="title-text">Garatéa</span></strong></h1>
<p class="lead">We are an initiative that focuses on <strong>increasing survival rates</strong> in medical emergencies.</p>
</div>
</div>
</div>
I am seeking advice on how to dynamically highlight the corresponding link in the navbar based on the section of the website currently being viewed. For instance, when on the 'about' or 'team' section, I envision adding an active
CSS class to the respective navbar link:
<li class="nav-item active"><a class="nav-link" href="#about">About</a></li>
.