Hello,
I am new to javascript, so I would need some help here
As you can see below, the code is written in a way, that when there is a link, by clicking on it, the behavior is set to scrool down to the section that has certain ID.
However, I have one (or more) links (a href) on my webpage, that I do not want to inherit this behavior, but to work as an actual link - meaning redirect to different page within my website.
Any thoughts how I can achieve that, what other element do I have to write in the js?
Thank you in advance
I am new to javascript, so I would need some help here
As you can see below, the code is written in a way, that when there is a link, by clicking on it, the behavior is set to scrool down to the section that has certain ID.
However, I have one (or more) links (a href) on my webpage, that I do not want to inherit this behavior, but to work as an actual link - meaning redirect to different page within my website.
Any thoughts how I can achieve that, what other element do I have to write in the js?
Thank you in advance
JavaScript:
////////////////////////////////////////////////////////
// Smooth scrooling animation //
const allLinks = document.querySelectorAll("a:link");
allLinks.forEach(function (link) {
link.addEventListener("click", function (e) {
e.preventDefault();
const href = link.getAttribute("href");
// Scroll back to top
if (href === "#")
window.scrollTo({
top: 0,
behavior: "smooth",
});
// Scroll to other links
if (href !== "#" && href.startsWith("#")) {
const sectionEl = document.querySelector(href);
sectionEl.scrollIntoView({ behavior: "smooth" });
}
});
});