Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

96
Vistas
Execute JS script if screen is certain size

I'm trying to create a menu bar on the side of my webpage that when a screen is a certain size it opens the menu up from the right by a certain amount.

  • If the screen is greater than 600 but less that 992, then it opens 250px.

  • If the screen is less that 600, then it opens at 100%.

So far I can get the JS to work by itself - but then I try to use 'if($(window).width() >= 600) {}' then none of it works (I have a button to open - but it loses its functionality with the below code.

Below is my initial working;

if($(window).width() >= 600) {
  function openNav() {
    document.getElementById("myMenu").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
    document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
  }
  function closeNav() {
    document.getElementById("myMenu").style.width = "";
    document.getElementById("main").style.marginLeft= "0";
    document.body.style.backgroundColor = "white";
  }
}

if($(window).width() < 600) {
  function openNav() {
    document.getElementById("myMenu").style.width = "100%";
    document.getElementById("main").style.marginLeft = "250px";
    document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
  }
  function closeNav() {
    document.getElementById("myMenu").style.width = "";
    document.getElementById("main").style.marginLeft= "0";
    document.body.style.backgroundColor = "white";
  }
}

I know that individually these work - but will work with all screen sizes.

  function openNav() {
    document.getElementById("myMenu").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
    document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
  }
  function closeNav() {
    document.getElementById("myMenu").style.width = "";
    document.getElementById("main").style.marginLeft= "0";
    document.body.style.backgroundColor = "white";
  }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

  1. You should declare the function outside the if block and can call them inside the if block. Right now you're declaring the function inside the if that could possibly the reason of code not working
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda