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

140
Visualizações
is it better to use javascript or css to swap out elements on screen size change for performance speed and less bugs

is ti better to use javascript, checking the window.innerwidth.

or is it better to have the display:none on media query

in terms of both lack of bugs and performance speed for simple situations.

for example:

you could either make a hook, or function that looks at the screen as ti changes or you could have a media query setting one to block and one to display none.

which one is faster and which one is less buggy?

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

0

In the case of check screen size and manipulate the style it's better using only CSS.

If you want to check the window width in JS you have to use something like a resize event attached to the window/document. And even if you wrap it in a debounce function it cost expensive calculations.

Also, there's the possibility that a user can have JavaScript disabled and your code didn't work at all.

If for some reasons (edit the content or functionality) you have to use JavaScript the better way is to use window.matchMedia() instead of the innerWidth function inside a resize event

Here more about it: https://webdevetc.com/blog/matchmedia-events-for-window-resizes/#windowmatchmedia-to-the-rescue

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