my application is built on Yii2 and bootstrap 4. I used a Bootstrap 4 "TheEvent" Theme (1 page) and managed to customize it into my multiple page website, but I've been stuck for days on the "Active" state menu. The "menu-active" state just won't change on whichever navigation menu I clicked. I've tried many solution I found here but none works.
Here's my navigation menu HTML :
<ul class="nav-menu">
<li class="menu-active"><a href="index">Home</a></li>
<li><a href="about">About</a></li>
<li><a href="news">News</a></li>
<li><a href="cabang">Cabang</a></li>
<li><a href="contact">Contact</a></li>
<?php if(Yii::$app->user->isGuest) { ?>
<li><a href="login">Login</a></li>
This is the JS in my layout file :
<?php
$script = <<< JS
$(document).ready(function() {
$(document).on('click', '.nav-menu a', function (e) {
$(this).parent().addClass('menu-active').siblings().removeClass('menu-active');
});
});
JS;
$this->registerJs($script)
?>
This is part of the main.js of the theme that I found relevant to the "menu-active" state :
// Smooth scroll for the menu and links with .scrollto classes
$('.nav-menu a, #mobile-nav a, .scrollto').on('click', function() {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var target = $(this.hash);
if (target.length) {
var top_space = 0;
if ($('#header').length) {
top_space = $('#header').outerHeight();
if( ! $('#header').hasClass('header-fixed') ) {
top_space = top_space - 20;
}
}
$('html, body').animate({
scrollTop: target.offset().top - top_space
}, 1500, 'easeInOutExpo');
if ($(this).parents('.nav-menu').length) {
$('.nav-menu .menu-active').removeClass('menu-active');
$(this).closest('li').addClass('menu-active');
}
if ($('body').hasClass('mobile-nav-active')) {
$('body').removeClass('mobile-nav-active');
$('#mobile-nav-toggle i').toggleClass('fa-times fa-bars');
$('#mobile-body-overly').fadeOut();
}
return false;
}
}
});
These are the JS asset. Most of them have something to do with animation, so I think it's irrelevant to the active-menu state
'lib/easing/easing.min.js',
'lib/superfish/hoverIntent.js',
'lib/superfish/superfish.min.js',
'lib/wow/wow.min.js',
'lib/venobox/venobox.min.js',
'lib/owlcarousel/owl.carousel.min.js',
'contactform/contactform.js',
'js/main.js'
I even deleted all the "main.js" file to see if my code will works, but it's still the same. The "menu-active" keep staying on my "home"/index menu.
Thank you in advance!