Guest User

Menu onscroll and onclick

a guest
Jun 8th, 2021
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. document.addEventListener('DOMContentLoaded', () => {
  2.   const allLinks = document.querySelectorAll('a[href^="#"');
  3.   const firstMenuLink = document.querySelectorAll(`${prefix}-header ${prefix}-menu-list__link`)[0];
  4.   const menuLine = document.querySelector(`${prefix}-header ${prefix}-menu__line`);
  5.   const menuItem = document.querySelectorAll(`${prefix}-header ${prefix}-menu-list__item`);
  6.   const menuLinks = document.querySelectorAll(`${prefix}-header ${prefix}-menu-list__link`);
  7.  
  8.   firstMenuLink.parentElement.classList.add(`${prefix.slice(1)}-menu-list__item--is-active`);
  9.   menuLine.style.left = `${firstMenuLink.offsetLeft}px`;
  10.   menuLine.style.width = `${firstMenuLink.offsetWidth}px`;
  11.  
  12.   allLinks.forEach((link) => {
  13.     link.addEventListener('click', (e) => {
  14.       e.preventDefault();
  15.  
  16.       let linkHref = link.getAttribute('href').slice(1);
  17.       const topOffset = (isFixedHeader || isStickyHeader) ? header.offsetHeight : 0;
  18.       const linkTarget = (linkHref) ? document.getElementById(linkHref) : null;
  19.       const linkTargetPos = (linkTarget) ? linkTarget.offsetTop : null;
  20.       const linkScrollOffset = (linkTargetPos) ? linkTargetPos - topOffset : null;
  21.  
  22.       if (link.classList.contains(`${prefix.slice(1)}-menu-list__link`)) {
  23.         menuItem.forEach((linkItem) => {
  24.           if (linkItem.classList.contains(`${prefix.slice(1)}-menu-list__item--is-active`)) {
  25.             linkItem.classList.remove(`${prefix.slice(1)}-menu-list__item--is-active`);
  26.           }
  27.         });
  28.  
  29.         link.parentElement.classList.add(`${prefix.slice(1)}-menu-list__item--is-active`);
  30.  
  31.         menuLine.style.left = `${link.offsetLeft}px`;
  32.         menuLine.style.width = `${link.offsetWidth}px`;
  33.       }
  34.  
  35.       window.scrollTo({
  36.         top: linkScrollOffset || 0,
  37.         behavior: 'smooth',
  38.       });
  39.     });
  40.  
  41.     document.addEventListener('scroll', () => {
  42.       const scrollDistance = window.scrollY;
  43.       const allSections = document.querySelectorAll('section');
  44.  
  45.       allSections.forEach((block, idx) => {
  46.         if (block.offsetTop - header.offsetHeight <= scrollDistance) {
  47.           if (link.parentElement.classList.contains(`${prefix.slice(1)}-menu-list__item--is-scroll-active`)) {
  48.             link.parentElement.classList.remove(`${prefix.slice(1)}-menu-list__item--is-scroll-active`);
  49.           }
  50.  
  51.           menuLinks[idx + 1].parentElement.classList.add(`${prefix.slice(1)}-menu-list__item--is-scroll-active`);
  52.           menuLine.style.left = `${menuLinks[idx + 1].offsetLeft}px`;
  53.           menuLine.style.width = `${menuLinks[idx + 1].offsetWidth}px`;
  54.         }
  55.       });
  56.     });
  57.   });
  58. });
Advertisement
Add Comment
Please, Sign In to add comment