-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from sammy0318/merge
updated js
- Loading branch information
Showing
1 changed file
with
30 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,44 @@ | ||
// Smooth scroll effect for navigation links | ||
document.querySelectorAll('a[href^="#"]').forEach(anchor => { | ||
anchor.addEventListener('click', function(e) { | ||
e.preventDefault(); | ||
|
||
document.querySelector(this.getAttribute('href')).scrollIntoView({ | ||
behavior: 'smooth' | ||
}); | ||
}); | ||
}); | ||
|
||
// Responsive menu toggle | ||
// Select DOM elements | ||
const menuToggle = document.querySelector('.menu-toggle'); | ||
const navLinks = document.querySelector('.nav-links'); | ||
const scrollToTopBtn = document.querySelector('.scroll-to-top'); // Make sure you have this element in your HTML | ||
|
||
// Toggle mobile menu | ||
menuToggle.addEventListener('click', () => { | ||
navLinks.classList.toggle('open'); | ||
// Optional: Toggle aria-expanded attribute for accessibility | ||
menuToggle.setAttribute( | ||
'aria-expanded', | ||
menuToggle.getAttribute('aria-expanded') === 'false' ? 'true' : 'false' | ||
); | ||
}); | ||
|
||
|
||
// Smooth Scrolling | ||
// Smooth scroll for all anchor links | ||
document.querySelectorAll('a[href^="#"]').forEach(anchor => { | ||
anchor.addEventListener('click', function(e) { | ||
e.preventDefault(); | ||
|
||
document.querySelector(this.getAttribute('href')).scrollIntoView({ | ||
behavior: 'smooth' | ||
}); | ||
const targetElement = document.querySelector(this.getAttribute('href')); | ||
|
||
if (targetElement) { | ||
targetElement.scrollIntoView({ | ||
behavior: 'smooth' | ||
}); | ||
|
||
// Close mobile menu when a link is clicked | ||
if (navLinks.classList.contains('open')) { | ||
navLinks.classList.remove('open'); | ||
menuToggle.setAttribute('aria-expanded', 'false'); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
|
||
|
||
scrollToTopBtn.addEventListener('click', () => { | ||
window.scrollTo({ top: 0, behavior: 'smooth' }); | ||
}); | ||
|
||
// Close mobile menu when a nav link is clicked | ||
navLinks.querySelectorAll('a').forEach(link => { | ||
link.addEventListener('click', () => { | ||
navLinks.classList.remove('open'); | ||
// Scroll to top functionality | ||
if (scrollToTopBtn) { | ||
scrollToTopBtn.addEventListener('click', () => { | ||
window.scrollTo({ | ||
top: 0, | ||
behavior: 'smooth' | ||
}); | ||
}); | ||
}); | ||
} |