Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved JS file smooth scrolling animation #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,37 @@ btnNavEl.addEventListener("click", function () {
////////////////////////////////////////////////
// Smooth scrolling animation

const allLinks = document.querySelectorAll("a:link");
const allLinks = document.querySelectorAll('a:link');

allLinks.forEach(function (link) {
link.addEventListener("click", function (e) {
allLinks.forEach(link => {
link.addEventListener('click', e => {
e.preventDefault();
const href = link.getAttribute("href");
const href = link.getAttribute('href');

// Scroll back to top
if (href === "#") {
if (href === '#') {
window.scrollTo({
top: 0,
behavior: "smooth",
behavior: 'smooth',
});
}

// Scroll to other links
if (href !== "#" && href.startsWith("#")) {
// Scroll to other sections
if (href.startsWith('#') && href !== '#') {
const sectionEl = document.querySelector(href);
sectionEl.scrollIntoView({
behavior: "smooth",
behavior: 'smooth',
});
}

// Close the mobile navigation
if (link.classList.contains("main-nav-link")) {
headerEl.classList.toggle("nav-open");
if (link.classList.contains('main-nav-link')) {
headerEl.classList.toggle('nav-open');
}
});
});


////////////////////////////////////////////////
// Sticky header

Expand Down