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

117
Visualizações
sliding menu hover effect in react js

https://codepen.io/shoumiksk/pen/PoJmQEN

This is the html css version of what i wanna achieve in React.

Here is my code in react:

function App() {
const marker = document.querySelector('#marker')
      const item = document.querySelectorAll('ul li a')
      function indicator(e){
        marker.style.top = e.offsetTop+'px';
        marker.style.width = e.offsetWidth+'px';
        
      }
  return (
    <div className="App">
    <ul>
      <div id="marker"></div>
      <li><a href="/" onMouseMove={(e)=> indicator(e.target)}>Home</a></li>
      <li><a href="/" onMouseMove={(e)=> indicator(e.target)}>about</a></li>
      <li><a href="/" onMouseMove={(e)=> indicator(e.target)}>services</a></li>
      <li><a href="/" onMouseMove={(e)=> indicator(e.target)}>contact</a></li>
      <li><a href="/" onMouseMove={(e)=> indicator(e.target)}>team</a></li>
    </ul>
    </div>
  );
}

CSS:

body {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
ul {
  position: relative;
}
ul li {
  list-style: none;
  font-size: 2rem;
}
ul li a {
  position: relative;
  text-decoration: none;
  padding: 0 1rem;
}
#marker {
  height: 3rem;
  position: absolute;
  top: 0;
  background-color: #2196f3;
  transition: 0.5s;
}

But this doesn't work as expected, sometimes it works after refreshing it doesn't.

I know i'm doing something wrong, any other way to do this?

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

0

Please check the below code to implement your required design for menu in react.

Here is my code:

export default function App() {
  const menuItem = [
    { i: 0, name: 'Home', link: '/' },
    { i: 1, name: 'about', link: '/' },
    { i: 2, name: 'services', link: '/' },
    { i: 3, name: 'contact', link: '/' },
    { i: 4, name: 'team', link: '/' },
  ];
  const [selected, setSelected] = useState(null);
  const item = document.querySelectorAll('ul li a');
  function indicator(index) {
    setSelected(index)
  }

  const renderMenu = menuItem.map((item, i) => {
    return (
      <ul>
        <li>
          <a href="/" className={selected==i ? 'isSelected':'menulist'} onMouseMove={(e) => indicator(i)}>
            {item.name}
          </a>
        </li>
      </ul>
    );
  });
  return <div className="App">{renderMenu}</div>;
}

.CSS

body {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: black;
}
ul {
  position: relative;
}
ul li {
  list-style: none;
  font-size: 2rem;
}
ul li a {
  position: relative;
  text-decoration: none;
  padding: 0 1rem;
  color: white;
}
.isSelected {
  height: 4rem;
  top: 0;
  background-color: #2196f3;
  transition: 0.5s ease;
}

You can put any required hover CSS in the isSelected class and it will work like a charm.

Here is my code demo link: https://stackblitz.com/edit/react-mewmxh?file=src%2Fstyle.css

about 4 years ago · Juan Pablo Isaza Relatório

0

So I was able to replicate that by using this function:

  let item;
  let marker;
  const indicator = (e) => {
    marker.style.top = e.offsetTop + "px";
    marker.style.width = e.offsetWidth + "px";
  };
  const getelements = () => {
    item = document.querySelectorAll("ul li a");
    marker = document.querySelector("#marker");
  };
  const fun = () => {   //Feel free to name whatever you want
    if (item === undefined || marker === undefined) {
      getelements();
    }
    if (item !== undefined && marker !== undefined) {
      item.forEach((link) => {
        link.addEventListener("mousemove", (e) => {
          indicator(e.target);
        });
      });
    }
  };

And the eventlistener:

<ul>
        <div id="marker"></div>
        <li>
          <a href="/" onMouseOver={() => fun()}>Home</a>
        </li>
        <li>
          <a href="/" onMouseOver={() => fun()}>about</a>
        </li>
        <li>
          <a href="/" onMouseOver={() => fun()}>services</a>
        </li>
        <li>
          <a href="/" onMouseOver={() => fun()}>contact</a>
        </li>
        <li>
          <a href="/" onMouseOver={() => fun()}>team</a>
        </li>
      </ul>

I'm using onMouseOver instead of onMouseMove as it was causing some jitters.

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