deepsite / main.js
samsalai's picture
Upload 19 files
ead63a3 verified
raw
history blame
3.18 kB
// Econovation Main JavaScript
document.addEventListener('DOMContentLoaded', function() {
// Elements
const navbar = document.querySelector('.econov-navbar');
const mobileToggle = document.querySelector('.mobile-toggle');
const mobileMenu = document.querySelector('.econov-mobile-menu');
const closeBtn = document.querySelector('.close-btn');
const overlay = document.querySelector('.econov-overlay');
// Set active nav link based on current page
const currentPage = window.location.pathname.split('/').pop();
const navLinks = document.querySelectorAll('.nav-link');
navLinks.forEach(link => {
const href = link.getAttribute('href');
if (href === currentPage ||
(currentPage === '' && href === 'index.html') ||
(currentPage === '/' && href === 'index.html')) {
link.classList.add('active');
}
});
// Navbar scroll effect
window.addEventListener('scroll', function() {
if (window.scrollY > 10) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
});
// Mobile menu toggle
if (mobileToggle) {
mobileToggle.addEventListener('click', function() {
mobileMenu.classList.add('open');
overlay.classList.add('open');
document.body.style.overflow = 'hidden'; // Prevent scrolling when menu is open
});
}
// Close mobile menu
if (closeBtn) {
closeBtn.addEventListener('click', closeMenu);
}
// Close menu when clicking overlay
if (overlay) {
overlay.addEventListener('click', closeMenu);
}
function closeMenu() {
mobileMenu.classList.remove('open');
overlay.classList.remove('open');
document.body.style.overflow = ''; // Restore scrolling
}
// Close menu when pressing escape key
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape' && mobileMenu.classList.contains('open')) {
closeMenu();
}
});
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
const targetId = this.getAttribute('href');
if (targetId === '#') return; // Skip if it's just '#'
e.preventDefault();
const targetElement = document.querySelector(targetId);
if (targetElement) {
// Close mobile menu if it's open
if (mobileMenu && mobileMenu.classList.contains('open')) {
closeMenu();
}
// Scroll to the element
const navbarHeight = navbar ? navbar.offsetHeight : 0;
const targetPosition = targetElement.getBoundingClientRect().top + window.pageYOffset - navbarHeight;
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
}
});
});
});