Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

78
Visualizações
Reusable javascript component issue

I have a woking accessible responsive navigation which for DEMO perpuses I have to make it reusable so I can show different sort of senarios. However tho, the click burger menu is not working.

This header with responsive navigation should work indipenently multiple times.

On click and matchMedia

  • typial responsive nav behavious with an extra touch of matchMedia in case user moves the window.
  • open navigation on click and remove navigation if > 900
  • navigation remove Attribute hidden on desktop and on click.

All of this should work multiple times.

if (navBlocks.length > 0){
        Array.prototype.forEach.call(navBlocks, function(el) {

the forEach function shoud do the work right? how come is not really working?

DEMO HERE

const navBlocks = document.querySelectorAll('.nav-container');
const nav = document.querySelector('.sliding-nav');
const menu = document.querySelector(".sliding-nav ul");
const toggleMenu = document.querySelector(".nav-container .nav-cta");
const mediaQuery = window.matchMedia('(min-width: 900px)');

let isMenuOpen = false;

if (navBlocks.length > 0) {
  Array.prototype.forEach.call(navBlocks, function(el) {

    el.addEventListener('click', e => {

      e.preventDefault();

      isMenuOpen = !isMenuOpen;

      toggleMenu.setAttribute('aria-expanded', String(isMenuOpen));
      menu.hidden = !isMenuOpen;

      if (isMenuOpen) {
        nav.classList.add('is-open');
        document.body.classList.add("is-no-scroll", "is-fixed");
        //console.log(isMenuOpen); 
      } else {
        nav.classList.remove('is-open');
        document.body.classList.remove("is-no-scroll", "is-fixed");
        //console.log(!isMenuOpen);
      }
    });

    function handleTabletChange(e) {
      // Check if the media query is true

      if (e.matches) {

        toggleMenu.setAttribute("aria-expanded", false);
        menu.removeAttribute("hidden");
        nav.classList.remove('is-open');
        document.body.classList.remove("is-no-scroll", "is-fixed");

      } else {
        toggleMenu.setAttribute("aria-expanded", false);
        //nav.removeAttribute("aria-expanded");

      }
    }

    // Register event listener
    mediaQuery.addListener(handleTabletChange);

    // Initial check
    handleTabletChange(mediaQuery);

  });
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you should do something like this, add event listener on burger menu icon to toggle an active class in nav bar and you should write css for that active class that if nav contains "active" class it should be set to display or else it should display none!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda