When I click on the mobile navbar, it simply doesn’t close when an item is clicked on, it only goes to the section where that item is but the navbar is still on:
here is the HTML for the header:
<header id="header" class="fixed-top d-flex align-items-center">
<div class="container d-flex align-items-center">
<h1 class="logo me-auto"><a href="#"><a href="index.html"><img src="logo.png" alt="elsahel">Logo</a></a></h1>
<!-- Uncomment below if you prefer to use an image logo -->
<!-- <a href="index.html" class="logo me-auto"><img src="assets/img/logo.png" alt="" class="img-fluid"></a>-->
<nav id="navbar" class="navbar">
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#clients">Our Suppliers</a></li>
<li><a href="#services">Our Services</a></li>
<li><a href="#customers">Clients</a></li>
<li><a href="#projects">Projects</a></li>
<li><a href="#contact">Contact Us</a></li>
</ul>
<i class="bi bi-list mobile-nav-toggle"></i>
<a href="index2.html" class="getstarted">Spanish</a>
</nav><!-- .navbar -->
</div>
</header><!-- End Header -->
CSS navbar code:
/*--------------------------------------------------------------
# Navigation Menu
--------------------------------------------------------------*/
/**
* Desktop Navigation
*/
.navbar {
padding: 0;
}
.navbar ul {
margin: 0;
padding: 0;
display: flex;
list-style: none;
align-items: center;
}
.navbar li {
position: relative;
}
.navbar a,
.navbar a:focus {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 0 10px 30px;
font-family: "Poppins", sans-serif;
font-size: 15px;
font-weight: 500;
color: #556270;
white-space: nowrap;
transition: 0.3s;
}
.navbar a i,
.navbar a:focus i {
font-size: 12px;
line-height: 0;
margin-left: 5px;
}
.navbar a:hover,
.navbar .active,
.navbar .active:focus,
.navbar li:hover>a {
color: #0609c9;
}
.navbar .getstarted,
.navbar .getstarted:focus {
background: #2600ce;
padding: 8px 25px;
margin-left: 30px;
border-radius: 4px;
font-weight: 400;
color: #fff;
}
.navbar .getstarted:hover,
.navbar .getstarted:focus:hover {
color: #fff;
background: #050aac;
}
.navbar .dropdown ul {
display: block;
position: absolute;
left: 14px;
top: calc(100% + 30px);
margin: 0;
padding: 10px 0;
z-index: 99;
opacity: 0;
visibility: hidden;
background: #fff;
box-shadow: 0px 0px 30px rgba(127, 137, 161, 0.25);
transition: 0.3s;
}
.navbar .dropdown ul li {
min-width: 200px;
}
.navbar .dropdown ul a {
padding: 10px 20px;
font-size: 15px;
text-transform: none;
font-weight: 400;
}
.navbar .dropdown ul a i {
font-size: 12px;
}
.navbar .dropdown ul a:hover,
.navbar .dropdown ul .active:hover,
.navbar .dropdown ul li:hover>a {
color: #1b07c7;
}
.navbar .dropdown:hover>ul {
opacity: 1;
top: 100%;
visibility: visible;
}
.navbar .dropdown .dropdown ul {
top: 0;
left: calc(100% - 30px);
visibility: hidden;
}
.navbar .dropdown .dropdown:hover>ul {
opacity: 1;
top: 0;
left: 100%;
visibility: visible;
}
@media (max-width: 1366px) {
.navbar .dropdown .dropdown ul {
left: -90%;
}
.navbar .dropdown .dropdown:hover>ul {
left: -100%;
}
}
/**
* Mobile Navigation
*/
.mobile-nav-toggle {
color: #556270;
font-size: 28px;
cursor: pointer;
display: none;
line-height: 0;
transition: 0.5s;
}
.mobile-nav-toggle.bi-x {
color: #fff;
}
@media (max-width: 991px) {
.mobile-nav-toggle {
display: block;
}
.navbar ul {
display: none;
}
}
.navbar-mobile {
position: fixed;
overflow: hidden;
top: 0;
right: 0;
left: 0;
bottom: 0;
background: rgba(63, 73, 83, 0.9);
transition: 0.3s;
z-index: 999;
}
.navbar-mobile .mobile-nav-toggle {
position: absolute;
top: 15px;
right: 15px;
}
.navbar-mobile ul {
display: block;
position: absolute;
top: 55px;
right: 15px;
bottom: 15px;
left: 15px;
padding: 10px 0;
background-color: #fff;
overflow-y: auto;
transition: 0.3s;
}
.navbar-mobile a,
.navbar-mobile a:focus {
padding: 10px 20px;
font-size: 15px;
color: #556270;
}
.navbar-mobile a:hover,
.navbar-mobile .active,
.navbar-mobile li:hover>a {
color: #0e0bd8;
}
.navbar-mobile .getstarted,
.navbar-mobile .getstarted:focus {
margin: 15px;
}
.navbar-mobile .dropdown ul {
position: static;
display: none;
margin: 10px 20px;
padding: 10px 0;
z-index: 99;
opacity: 1;
visibility: visible;
background: #fff;
box-shadow: 0px 0px 30px rgba(127, 137, 161, 0.25);
}
.navbar-mobile .dropdown ul li {
min-width: 200px;
}
.navbar-mobile .dropdown ul a {
padding: 10px 20px;
}
.navbar-mobile .dropdown ul a i {
font-size: 12px;
}
.navbar-mobile .dropdown ul a:hover,
.navbar-mobile .dropdown ul .active:hover,
.navbar-mobile .dropdown ul li:hover>a {
color: #2d17f3;
}
.navbar-mobile .dropdown>.dropdown-active {
display: block;
}
The whole javascript code:
(function() {
"use strict";
/**
* Easy selector helper function
*/
const select = (el, all = false) => {
el = el.trim()
if (all) {
return [...document.querySelectorAll(el)]
} else {
return document.querySelector(el)
}
}
/**
* Easy event listener function
*/
const on = (type, el, listener, all = false) => {
let selectEl = select(el, all)
if (selectEl) {
if (all) {
selectEl.forEach(e => e.addEventListener(type, listener))
} else {
selectEl.addEventListener(type, listener)
}
}
}
/**
* Easy on scroll event listener
*/
const onscroll = (el, listener) => {
el.addEventListener('scroll', listener)
}
/**
* Toggle .header-scrolled class to #header when page is scrolled
*/
let selectHeader = select('#header')
if (selectHeader) {
const headerScrolled = () => {
if (window.scrollY > 100) {
selectHeader.classList.add('header-scrolled')
} else {
selectHeader.classList.remove('header-scrolled')
}
}
window.addEventListener('load', headerScrolled)
onscroll(document, headerScrolled)
}
/**
* Back to top button
*/
let backtotop = select('.back-to-top')
if (backtotop) {
const toggleBacktotop = () => {
if (window.scrollY > 100) {
backtotop.classList.add('active')
} else {
backtotop.classList.remove('active')
}
}
window.addEventListener('load', toggleBacktotop)
onscroll(document, toggleBacktotop)
}
/**
* Hero carousel indicators
*/
let heroCarouselIndicators = select("#hero-carousel-indicators")
let heroCarouselItems = select('#heroCarousel .carousel-item', true)
heroCarouselItems.forEach((item, index) => {
(index === 0) ?
heroCarouselIndicators.innerHTML += "<li data-bs-target='#heroCarousel' data-bs-slide-to='" + index + "' class='active'></li>":
heroCarouselIndicators.innerHTML += "<li data-bs-target='#heroCarousel' data-bs-slide-to='" + index + "'></li>"
});
/**
* Porfolio isotope and filter
*/
window.addEventListener('load', () => {
let portfolioContainer = select('.portfolio-container');
if (portfolioContainer) {
let portfolioIsotope = new Isotope(portfolioContainer, {
itemSelector: '.portfolio-item'
});
let portfolioFilters = select('#portfolio-flters li', true);
on('click', '#portfolio-flters li', function(e) {
e.preventDefault();
portfolioFilters.forEach(function(el) {
el.classList.remove('filter-active');
});
this.classList.add('filter-active');
portfolioIsotope.arrange({
filter: this.getAttribute('data-filter')
});
}, true);
}
});
/**
* Initiate portfolio lightbox
*/
const portfolioLightbox = GLightbox({
selector: '.portfolio-lightbox'
});
/**
* Portfolio details slider
*/
new Swiper('.portfolio-details-slider', {
speed: 400,
loop: true,
autoplay: {
delay: 5000,
disableOnInteraction: false
},
pagination: {
el: '.swiper-pagination',
type: 'bullets',
clickable: true
}
});
/**
* Initiate portfolio details lightbox
*/
const portfolioDetailsLightbox = GLightbox({
selector: '.portfolio-details-lightbox',
width: '90%',
height: '90vh'
});
/**
* Skills animation
*/
let skilsContent = select('.skills-content');
if (skilsContent) {
new Waypoint({
element: skilsContent,
offset: '80%',
handler: function(direction) {
let progress = select('.progress .progress-bar', true);
progress.forEach((el) => {
el.style.width = el.getAttribute('aria-valuenow') + '%'
});
}
})
}
})()
It seems that the code doesn’t include to close the mobile navbar when clicked on.
I have tried several code from stackoverflow but they don’t seem to work.
I have tried to add a code from ChatGPT answers, it told me that I need to change the behavior of on and select functions and removed them and used normal selectQuery methods but they didn’t work. tried a couple of answers from Stackoverflow, but they don’t also seem to work.
2
Answers
After various attempts to question ChatGPT, i managed to get the answer that worked for me, here is the answer for chatGPT one: To add the close functionality to the mobile navbar when an "a" tag is clicked, you can modify the JavaScript code as follows:
Add a function to close the mobile navbar:
Modify the event listener for the mobile nav dropdowns to include the close functionality:
Add an event listener to close the mobile navbar when any "a" tag is clicked inside the navbar:
Regarding CSS, you may need to add the necessary styles to ensure the close functionality works properly. Make sure that the CSS selectors and classes used in the JavaScript code match the corresponding elements in your HTML markup.
I hope this helps for you as well
As I can see bootstrap classes, then why you need to write custom js..Use the bootstrap navbar and it automatically will work. Sometimes it happen if its not able to locate js..
make sure:
#Apache-Age