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

84
Visualizações
Prevent scroll bar to go up when click mobile nav

I have a mobile nav and I open it using this code in JS file:

var toggleButton = document.querySelector(".toggle-button");
var mobileNav = document.querySelector(".mobile-nav");
var closeButton = document.querySelector(".close-button");

toggleButton.addEventListener("click", function () {
  mobileNav.classList.add("slide");
});

closeButton.addEventListener("click", function () {
  mobileNav.classList.remove("slide");
});
.mobile-nav {
  position: fixed;
  transition: 0.4s ease;
  transform: translateX(1400px);
}

.slide {
  transform: translateX(0);
}

when I scroll down and open mobile nav using slide class the scroll bar goes up, how to fix it? i want it to remain at current place where I open slide menu.

here is project Github page for more codes: enter link description here

and here is the online website for reprodure the issue, just make browser page smaller than 768px and scroll down then tap hamburger icon to see the issue.

enter link description here

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

0

const navSection = document.querySelector(".nav-section");

function openNav() {
  navSection.classList.add("open-nav")
}

function closeNav() {
  navSection.classList.remove("open-nav")
}
.nav-section{
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  opacity: 0;
  transition: 0.25s;
  pointer-events: none;
}

.nav-section nav{
  display: flex;
  flex-direction: column;
  position: relative;
  transform: translateX(-100%);
  width: 20rem;
  height: 100%;
  background: red;
}

.open-nav{
  pointer-events: all;
  opacity: 1;
  background: rgba(0, 0, 0, 0.5);
}

.open-nav nav{
  transform: translateX(0%);
}

.close-nav-btn{
  position: absolute;
  right: 0;
  top: 0;
}
<div class="open-btn">
  <button onclick="openNav()">open</button>
</div>

<section class="nav-section">
  <nav>
    <a>nav 0</a>
    <a>nav 1</a>
    <a>nav 2</a>
    <a>nav 3</a>
  </nav>
  <button onclick="closeNav()" class="close-nav-btn">close</button>
</section>

Like this maybe?

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