How to set "activate" class to bootstrap navbar in Angular 2 while using page anchor navigation? - html

I am creating a single page website using Angular 2 and Bootstrap 4.
I created a navbar component that is always on top of the page and I am using page anchor navigation (#id). Please note that I haven't created a routing module (so far it hasn't been necessary to do this).
The navbar code is 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">Início</a></li>
<li class="nav-item"><a class="nav-link" href="#about">Sobre</a></li>
<li class="nav-item"><a class="nav-link" href="#team">Quem Somos</a></li>
<li class="nav-item"><a class="nav-link" href="#contact">Contato</a></li>
</ul>
</div>
</div>
</nav>
Each link corresponds to a section of the single page website, for example, the home component template is:
<div id="home" class="text-center home-background">
<div class="overlay">
<div class="content">
<h1>Bem vindo à <strong><span class="title-text">Garatéa</span></strong></h1>
<p class="lead">Somos uma inciativa que <strong>aumenta a taxa de sobrevivência</strong> em emergências médicas.</p>
</div>
</div>
</div>
My question is: how can I highlight the link in the navbar that corresponds to the section of the website that I am currently browsing? For example, if I am looking at the team about section, I expect to have an active CSS class on the navbar: <li class="nav-item active"><a class="nav-link" href="#about">Sobre</a></li>.

You can use the srcElement on click of that <a> tag as below
<a class="nav-link" (click)="clickedLink($event)">Início</a>
<a class="nav-link" (click)="clickedLink($event)">Início</a>
<a class="nav-link" (click)="clickedLink($event)">Início</a>
Method will be as
clickedLink(e){
let element = e.srcElement;
console.log(e)
for(let i =0; i <element.parentElement.children.length ; i++){
if(element.parentElement.children[i].classList.contains('active')){
element.parentElement.children[i].classList.remove('active')
}
}
element.classList.add('active');
}
LIVE DEMO

Related

Scrollspy active not working with bootsrap 5

I need a help. I used the code from Bootstrap example but it didn't work.
I put data target on Javascript, and i also put data-target on body.
The code is full copy from bootstrap-5 scrollspy, but i don't completely understand how to use it. Thank you for your help.
$('body').scrollspy({ target: '#navbar-example2' });
<body data-target="#navbar-example2">
<nav id="navbar-example2" class="navbar navbar-light bg-light px-3 fixed-top">
<a class="navbar-brand" href="#">Navbar</a>
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" href="#scrollspyHeading1">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#scrollspyHeading2">About Me</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#scrollspyHeading3">Skills</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#scrollspyHeading4">My Project</a>
</li>
</ul>
</nav>
<div data-bs-spy="scroll" data-bs-target="#navbar-example2" data-bs-offset="0" class="scrollspy-example" tabindex="0">
<section id="scrollspyHeading1">
</section>
<section id="scrollspyHeading2">
</section>
<section id="scrollspyHeading3">
</section>
<section id="scrollspyHeading4">
</section>
</div>
</body>
As explained in the Bootstrap 5 docs..
"Scrollable containers and keyboard access If you’re making a
scrollable container (other than the ), be sure to have a height
set and overflow-y: scroll;..."
For example,
.scrollspy-example {
position: relative;
height: 300px;
overflow: auto;
}
Demo

How do I get my linked HTML pages to redirect properly and not take me deeper in?

I have been trying to link html pages created on bootstrap without success. The buttons don't even click out so it's not like I get taken to another page with an error message. Just nothing. But today I observed something that might help someone more savvy than me identify the problem.
So, I'm trying to link other pages on the index.html (as an example, the services page).
Instead of taking me to "https://chozeqfarms.dashnexpages.net/services/" it looks like it wants to take me to https://chozeqfarms.dashnexpages.net/index/services.html, which I observe when hovering over the service button.
My code snippet for the navigation bar is below, and the href looks regular to my untrained eyes. But I must be doing something wrong, please what could be wrong and how do I fix it?
<div class="row navbar-row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 navbar-container mynav">
Chozeq Farms
<nav class="navbar navbar-full">
<div class="collapse navbar-toggleable-md" id="tmNavbar">
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link active" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="services.html">Services</a>
</li>
<li class="nav-item">
<a class="nav-link" href="trainings.html">Trainings</a>
</li>
<li class="nav-item">
<a class="nav-link" href="contact.html">Contact</a>
</li>
</ul>
</div>
</nav>
<button class="navbar-toggler hidden-lg-up" type="button" data-toggle="collapse" data-target="#tmNavbar">
☰
</button>
</div>
</div>

In Bootstrap, how do I change what color the links are in my navbar when hovering over them?

Please help, it's driving me crazy (it's insane that I have spent so much time on the navbar text color overall).
I'm just learning web development so I just figured out how to change the text color in my navbar and now it turns an ugly blue when I hover over it. How do I figure out how to fix that?
I know others have asked this before but they all get specific answers that don't apply to my code and I don't understand why the solutions work, so I could figure it out by myself.
Thanks for your help!
<!--Navbar-->
<nav class="navbar navbar-expand-lg yellow lighten-3 fixed-top scrolling-navbar">
<div class="container">
<!--center the navbar content-->
<!-- Navbar brand -->
<a class="navbar-brand navbar-text-color-custom" href="#">Pawsitive Lead</a>
<!-- Collapse button -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#basicExampleNav"
aria-controls="basicExampleNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Collapsible content -->
<div class="collapse navbar-collapse" id="basicExampleNav">
<!-- Links - each sends you to the specific section of the page -->
<ul class="navbar-nav mr-auto smooth-scroll">
<li class="nav-item">
<a class="nav-link navbar-text-color-custom" href="#intro">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link navbar-text-color-custom" href="#best-features">Kdo smo?</a>
</li>
<li class="nav-item">
<a class="nav-link navbar-text-color-custom" href="#examples">Izkušnje</a>
</li>
<li class="nav-item">
<a class="nav-link navbar-text-color-custom" href="#gallery">V razmislek</a>
</li>
<li class="nav-item">
<a class="nav-link navbar-text-color-custom" href="#contact">Pišite nam</a>
</li>
</ul>
<!-- Links -->
<form class="form-inline">
<div class="md-form my-0">
<input class="form-control mr-sm-2" type="text" placeholder="Išči" aria-label="Search">
</div>
</form>
</div>
<!-- Collapsible content -->
</div>
</nav>
<!--/.Navbar-->
You can approach it by different ways ..
define a separate class in a separate css stylesheet and override the color by using !important :
e.g : make a class newClass in newCss.css
.newClass:hover{
color: red !important; /*red is an example, put whatever you want*/
}
define a style tag in the html :
<style>
.newClass:hover{
color: red !important; /*red is an example, put whatever you want*/
}
</style>
in HTML , put the class inside the anchor:
<li class="nav-item">
<a class=" newClass nav-link navbar-text-color-custom" href="#best-features">Kdo smo?</a></li>
By using !important you ensure that the css color red is applied to the anchor.

Bootstrap - Position 1 item to the left while preserving another item's center position

I cannot think of a way to have the logo on the left of the navbar but for the items to be perfectly centered. I tried using margin auto for the items but then they move further right away from the center because of the logo taking up space.
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<div class="collapse navbar-collapse justify-content-center d-flex">
<a class="navbar-brand">WIDE-LOGO-TEXT</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link">Home</a>
</li>
<li class="nav-item">
<a class="nav-link">Gallery</a>
</li>
<li class="nav-item">
<a class="nav-link">Services</a>
</li>
<li class="nav-item">
<a class="nav-link">Contact Us</a>
</li>
</ul>
</div>
</nav>
<!-- Second NavBar: -->
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<div class="collapse navbar-collapse d-flex">
<a class="navbar-brand">WIDE-LOGO-TEXT</a>
<ul class="navbar-nav" style="margin: 0 auto;">
<li class="nav-item">
<a class="nav-link">Home</a>
</li>
<li class="nav-item">
<a class="nav-link">Gallery</a>
</li>
<li class="nav-item">
<a class="nav-link">Services</a>
</li>
<li class="nav-item">
<a class="nav-link">Contact Us</a>
</li>
</ul>
</div>
</nav>
https://www.codeply.com/go/DUmlhXetYE
As you can see from the preview link above, the 2nd navbar items are in the exact same position at the first navbar items. The problem is that I want these to be perfectly centered as though the logo was never there. Therefore I need the nav items to shift to the left to be perfectly centered and unaffected by the logo.
How can I do this? Hope that makes sense!
EDIT: Image to illustrate issue more clearly:
There are 2 way to fix your problem.
1) Logo class apply css position: absolute and parent class apply css position: relative;
2) Menu Class navbar-nav apply css transform: translateX(-80px); you can get navigation center align. as per your expectation.
Use absolute positioning:
.navbar-brand {
position: absolute;
left: 16px;
}
Please use Bootstrap 4 for this kind of predefined library of css.
Copy paste the below code you found the result
enter image description here
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-expand-sm">
<button class="navbar-toggler mr-2" type="button" data-toggle="collapse" data-target="#navbar">
<span class="navbar-toggler-icon"></span>
</button>
<span class="navbar-brand d-flex flex-fill">WIDE-LOGO-TEXT</span>
<div class="navbar-collapse collapse" id="navbar">
<ul class="navbar-nav justify-content-center d-flex flex-fill">
<li class="nav-item">
<a class="nav-link">Home</a>
</li>
<li class="nav-item">
<a class="nav-link">Gallery</a>
</li>
<li class="nav-item">
<a class="nav-link">Services</a>
</li>
<li class="nav-item">
<a class="nav-link">Contact Us</a>
</li>
</ul>
</div>
<div class="d-flex flex-fill"><!--spacer--> </div>
</nav>
<div class="container-fluid">
<h5 class="text-center">--center--</h5>
</div>
</body>
</html>
Best of luck :)

Change Active tab in navbar when router.navigate is used

I'm using Angular 2 with Bootstrap 4. I can get the active tab to change using the following code:
navbar.component.html
<nav class="navbar navbar-fixed-top navbar-light bg-faded">
<button class="navbar-toggler hidden-sm-up" type="button" data-toggle="collapse" data-target="#navbar">
☰
</button>
<div class="collapse navbar-toggleable-xs" id="navbar">
<div class="container">
<a class="navbar-brand" [routerLink]="['']">My App</a>
<ul class="nav navbar-nav navbar-right">
<li class="nav-item" routerLinkActive="active" [routerLinkActiveOptions]="{exact:
true}">
<a class="nav-link" [routerLink]="['']">Home<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
<a class="nav-link" [routerLink]="['/about']">About</a>
</li>
<li class="nav-item" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
<a class="nav-link" [routerLink]="['/example']">Example</a>
</li>
</ul>
</div>
</div>
</nav>
The line:
[routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}"
works well at changing to the currently active tab, but when I navigate to a component using something like:
this.router.navigate(['']);
The active tab doesn't change. Is there a way to change the active tab when navigating like above?
So basically you want to make your high-lighting in a service and call that service to apply your style when your URL equals a specific state. this is done by using
this.router.url === '/myroute'
here is my plunker. NOTE that the active tab will change whether you use the links or the buttons. The buttons are what use the this.router.navigate(['']); like you asked in the question