1

I'd like to toggle stylein JavaScript. Not familiar with JavaScript but could I somehow toggle this:

document.body.style.overflow = 'hidden' 

It's part of this function:

<a id="mobile-nav" href="#">
  <div class="container1" onclick="myFunction(this)">
    <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>
  </div>
</a>  
function myFunction(x) {
  x.classList.toggle('change');
  document.body.style.overflow = 'hidden' 
} 

I'd like it to apply when I press it, and not apply when I press it again. At the moment it applies when i initiate the function, but doesn't stop applying when I retoggle the function.

1 Answer 1

2
document.body.style.overflow = document.body.style.overflow ? null : 'hidden';
Sign up to request clarification or add additional context in comments.

4 Comments

That would turn it on and off?
Yes, if overflow exist it will be remove, else will set 'hidden'
Yep. Brilliant.
not brillant, you forget x.classList.toggle('change') return a boolean value, usable to directly set the overflow toggle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.