简体   繁体   中英

How to undo javascript function

I have a function like so which toggles my dropdown menu.

<div class="module widget-handle mobile-toggle right visible-sm visible-xs">
  <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>
</div>
<script>
  function myFunction(x) {
    x.classList.toggle('change');
    document.body.style.overflow ='hidden'
  }
</script>

The last part of it

document.body.style.overflow = 'hidden'

Keeps the page from overflowing at the top when the dropdown menu is open. The problem is that when I click on my menu bars, whilst the overflow disappears, clicking on the bars again reveals the page but locks it due to the overflow being hidden.

I'd like to undo

document.body.style.overflow = 'hidden'

When the menu is closed and the closest I've got to understanding it is the function doBack . Can I somehow add doBack to my existing function?

Mobile page here

It's a WordPress site and the javascript above toggles the dropdown by pressing on the bars. In order to extend the dropdown, I made the class collapse bigger, like so.

@media (max-width: 768px) {
  .collapse {
    position: absolute;
    height: 775px;
    background-color: white;
    z-index: 99999 !important;
    top: 75px;
    left: -50px;
    line-height: 10px;
  }
}

HTML:

<div class="module-group right">
  <div class="module left">
    <div class="collapse navbar-collapse navbar-ex1-collapse">
      <ul id="menu" class="menu">
        <li id="menu-item-15050" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-15050 dropdown">
          <a title="Contact" href="url">Contact
            <ul role="menu" class="dropdow n-menu">
            </ul>
          </a>
        </li>
      </ul>
    </div>
  </div>
</div>

You can eliminate the style attribute by using:

document.body.style.overflow = null

You can simply add a new CSS class:

.body-overflow {
  overflow: hidden;
}

and in your javascript function add another .classList.toggle() but this time on the body:

function myFunction(x) {
  x.classList.toggle('change');
  document.body.classList.toggle('body-overflow');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM