skrollmenu by HAP

by Petr Haluza

HTML

<h1>test <br>skrolovaciho <br>menu</h1>
<section>by HAP</section>
<skrollmenu>
    <nav>
      <a href="#popis" style="--clr:#fb9da7;" class="active">Popis</a>
      <a href="#vykon" style="--clr:#fad4ae;">Herni vykon</a>
      <a href="#Parametry" style="--clr:#88c59a;">Parametry</a>
      <a href="#Varianty" style="--clr:#d0c6e9;">Varianty</a>
      <a href="#Podobne" style="--clr:#88c5fa;">Podobne</a>
    </nav>
  </skrollmenu>
	
  <section id="popis" class="scrollBlok" style="--clr:#fb9da7;">Popis</section>
  <section id="vykon" class="scrollBlok" style="--clr:#fad4ae;">vykon</section>
  <section id="Parametry" class="scrollBlok" style="--clr:#88c59a;">Parametry</section>
  <section id="Varianty" class="scrollBlok" style="--clr:#d0c6e9;">Varianty</section>
  <section id="Podobne" class="scrollBlok" style="--clr:#88c5fa;">Podobne</section>

CSS

* { margin: 0; padding: 0; box-sizing: border-box; scroll-behavior: smooth;}

section.scrollBlok  {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 8em;
  font-weight: 800;
  color: rgba(0,0,0,0.25);
  text-transform: uppercase;
  background: var(--clr);
}

skrollmenu {
  position: sticky;
  top: 0;
  width: 100%;
  background: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 50px 0;
}



skrollmenu nav {
  display: flex;
  gap: 10px;
}
skrollmenu nav a {
  position: relative;
  text-decoration: none;
  padding: 12px 20px;
  color: #fff;
  font-weight: 500;
}
skrollmenu nav a.active {
  background: var(--clr);
  color: #333;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}
skrollmenu nav a.active::before {
  content: '';
  position: absolute;
  left: -20px;
  bottom: 0;
  width: 20px;
  height: 20px;
  background: transparent;
  border-bottom-right-radius: 20px;
  box-shadow: 5px 5px 0 5px var(--clr);
}
skrollmenu nav a.active::after {
  content: '';
  position: absolute;
  right: -20px;
  bottom: 0;
  width: 20px;
  height: 20px;
  background: transparent;
  border-bottom-left-radius: 20px;
  box-shadow: -5px 5px 0 5px var(--clr);
}

JavaScript

let sec = document.querySelectorAll('section.scrollBlok');
let links = document.querySelectorAll('skrollmenu nav a');

window.onscroll = () => {
  sec.forEach(section => {
    let top = window.scrollY;
    let offset = section.offsetTop - 60;
    let height = section.offsetHeight;
    let id = section.getAttribute('id');

    if(top >= offset && top < offset + height){
      links.forEach(link => {
        link.classList.remove('active');
        document.querySelector('nav a[href*=' + id + ']').classList.add('active');
      })
    }
  })
};