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

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

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 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!