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

347
Visualizações
Navigation sliding line on hover (and active state)

I'd spinning this off this original post: https://stackoverflow.com/a/65040903/1406440

I used something similar years ago (https://codepen.io/moy/pen/pZdjMX) but it uses a lot of jQuery in comparison. So I was wondering if I can amend the example in the original post to achieve the same effect. Which would mean...

  • Updating so that if there was an 'active' item, the line is already visible and moves from that location.
  • Is it possible to apply to a class rather than id?
  • With the above in mind, could this be applied in two separate places or would I need to duplicate the code?
  • Finally I wonder if we could use a :before or :after class so I don't have a div floating in the ul?

const navBar = document.getElementById("nav");
const navCursor = navBar.querySelector('.cursor');
const navItems = navBar.querySelectorAll('li');

function handleMouseEnterNavItem(event) {
  // executed when mouse enter a navigation item
  // update cursor to match position and size of target
  const { offsetLeft, clientWidth } = event.target;
  navCursor.style.left = offsetLeft + 'px';
  navCursor.style.width = clientWidth + 'px';
}

navItems.forEach((navItem) => {
  navItem.addEventListener('mouseenter', handleMouseEnterNavItem);
});
ul {
  position: relative;
}

ul li {
  font-family: sans-serif;
  text-decoration: none;
  color: gray;
  margin: 22px 10px 10px;
  display: inline-block;
  padding: 0; /* note: padding will be underlined */
}

ul .cursor {
  background: blue;
  height: 1px;
  position: absolute;
  bottom: 0;
  z-index: 10;
  transition: .16s all 0.025s;
}

ul a {
  text-decoration: none;
}
<ul id="nav" class="tabs-nav">
  <li class="active"><a href="#">News</a></li>
  <li><a href="#">Activities</a></li>
  <li><a href="#">Search</a></li>
  <li><a href="#">Time</a></li>
  <div class="cursor"></div>
</ul>

about 4 years ago · Juan Pablo Isaza
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