Dear friends,
I would like to toggle the button in the navbar for mobile RWD. I already did it with JavaScript, but I don’t know how to do that with Angular. Can someone help me please? Thanks
Here are the codes bellow:
<!-- HTML-->
<nav>
<div class="nav-center">
<div class="nav-header">
<h3>LOGO</h3>
<button class="nav-toggle">
<i class="fas fa-bars"></i>
</button>
</div>
<ul class="links">
<li>
<a href="#about">About</a>
</li>
<li>
<a href="#shopping">Coffee</a>
</li>
<li>
<a href="#contact">Contact</a>
</li>
</ul>
<ul class="social-icons">
<li>
<a href="#">
<i class="fab fa-behance"></i>
</a>
</li>
<li>
<a href="#">
<i class="fab fa-sketch"></i>
</a>
</li>
<li>
<a href="#">
<i class="fab fa-youtube"></i>
</a>
</li>
<li>
<a href="#">
<i class="fab fa-linkedin"></i>
</a>
</li>
</ul>
</div>
</nav>
// JavaScript
const navToggle = document.querySelector(".nav-toggle");
const links = document.querySelector(".links");
navToggle.addEventListener("click", function() {
links.classList.toggle("show-links");
});
/*CSS*/
nav {
background: #bf935a;
}
.nav-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 1rem;
}
.nav-toggle {
font-size: 1.5rem;
color: #8b4513;
background: transparent;
border-color: transparent;
transition: all 0.3s linear;
cursor: pointer;
}
.nav-toggle:hover {
color: #815e32;
transform: rotate(90deg);
}
.logo {
height: 80px;
}
.links a {
color: #ffffff;
font-size: 1rem;
text-transform: capitalize;
letter-spacing: 0.25rem;
display: block;
padding: 0.5rem 1rem;
transition: all 0.3s linear;
}
.links a:hover {
color: #8b4513;
padding-left: 1.5rem;
}
.social-icons {
display: none;
}
.links {
height: 0;
overflow: hidden;
transition: all 0.3s linear;
}
.show-links {
height: 10rem;
}
@media (min-width: 800px) {
.nav-center {
max-width: 1170px;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: space-between;
padding: 1rem;
}
.nav-header {
padding: 0;
}
.nav-toggle {
display: none;
}
.links {
height: auto;
display: flex;
margin-left: 5em;
}
.links a {
padding: 0;
margin: 0 0.5rem;
}
.links a:hover {
padding: 0;
background: transparent;
}
.social-icons {
display: flex;
}
.social-icons a {
margin: 0 0.5rem;
color: #8b4513;
transition: all 0.3s linear;
}
.social-icons a:hover {
color: #815e32;
}
}
Angular
<!--HTML-->
<button [ngClass]="showLinks() ? 'show-links': ''" (click)="click()" class="nav-toggle">
<i class="fas fa-bars"></i>
</button>
// TS
showLinked: boolean = false;
showLinks() {
return this.showLinked;
}
click() {
this.showLinked = !this.showLinked
}