Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Ejecute el script JS si la pantalla tiene un tamaño determinado

Estoy tratando de crear una barra de menú en el costado de mi página web que cuando una pantalla tiene cierto tamaño, abre el menú desde la derecha en una cierta cantidad.

  • Si la pantalla es mayor a 600 pero menor a 992, entonces abre 250px.

  • Si la pantalla es inferior a 600, entonces se abre al 100%.

Hasta ahora puedo hacer que el JS funcione solo, pero luego trato de usar 'if ($ (ventana). ancho ()> = 600) {}', entonces nada de eso funciona (tengo un botón para abrir - pero pierde su funcionalidad con el siguiente código.

A continuación se muestra mi trabajo inicial;

 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"; } }

Sé que estos funcionan individualmente, pero funcionarán con todos los tamaños de pantalla.

 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 answers
Answer question

0

  1. Debe declarar la función fuera del bloque if y puede llamarla dentro del bloque if. En este momento, está declarando la función dentro del si esa podría ser la razón por la que el código no funciona
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!