A demo web view to scroll down and up folloing the header menu. The page is not reponsive but you can get it as example to set up a better one.
As you can see the function goToSectionPage
is responsable to do the event to get the scroll to the wish section.
Also, you can optimize the repetition of function addEventHandlerToHeaderButton
in the js code.
function goToSectionPage(sectionElement) {
sectionElement.scrollIntoView({ behavior: "smooth", block: "center" });
}
function addEventHandlerToHeaderButton(buttonId, sectionId) {
const buttonTarget = document.getElementById(buttonId);
buttonTarget.addEventListener("click", function () {
const parentSectionElement = document.querySelector("main");
const targetSection = document.getElementById(sectionId);
goToSectionPage(targetSection);
addCssClassToSelecedtSession(parentSectionElement, targetSection, "selected");
});
}
function addCssClassToSelecedtSession(parentWrapSectionElements, selectedSectionElement, className) {
for(let section of parentWrapSectionElements.children){
section.classList.remove(className)
}
selectedSectionElement.classList.add(className)
}
addEventHandlerToHeaderButton("headerSection1", "section1");
addEventHandlerToHeaderButton("headerSection2", "section2");
addEventHandlerToHeaderButton("headerSection3", "section3");
addEventHandlerToHeaderButton("headerSection4", "section4");
addEventHandlerToHeaderButton("headerSection5", "section5");
addEventHandlerToHeaderButton("headerSection6", "section6");
addEventHandlerToHeaderButton("headerSection7", "section7");
addEventHandlerToHeaderButton("headerAbout", "about");
.section {
display: block;
height: 50px;
border: 1px solid black;
border-radius: 5px;
margin-bottom: 10px;
padding: 5px;
}
.header-item {
cursor: pointer;
padding: 0 8px;
font-family: system-ui;
font-size: 10px;
color: #00000080;
}
.header-item:hover {
border-bottom: 1px solid black;
}
header {
width: 100%;
position: fixed;
}
main {
padding-top: 45px;
}
.selected {
animation: sectionSelected 2s forwards;
}
@keyframes sectionSelected {
from {
background-color: #fff;
}
to {
background-color: #c4c4c4;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test</title>
<link rel="stylesheet" href="./stackoverflow.css">
</head>
<body>
<header>
<span id="headerSection1" class="header-item">Go to Section 1</span>
<span id="headerSection2" class="header-item">Go to Section 2</span>
<span id="headerSection3" class="header-item">Go to Section 3</span>
<span id="headerSection4" class="header-item">Go to Section 4</span>
<span id="headerSection5" class="header-item">Go to Section 5</span>
<span id="headerSection6" class="header-item">Go to Section 6</span>
<span id="headerSection7" class="header-item">Go to Section 7</span>
<span id="headerAbout" class="header-item">Go to About</span>
</header>
<main>
<section id="section1" class="section">Section1</section>
<section id="section2" class="section">Section2</section>
<section id="section3" class="section">Section3</section>
<section id="section4" class="section">Section4</section>
<section id="section5" class="section">Section5</section>
<section id="section6" class="section">Section6</section>
<section id="section7" class="section">Section7</section>
<section id="about" class="section">About</section>
</main>
</body>
<script src="./stackoverflow.js"></script>
</html>
https://github.com/stembrino/simple-page-example-scroll-down-up