user13711618
user13711618

Reputation:

How can I unable scroll in javascript?

When I created navbar on my project I noticed that I can scroll on the website

and what I decided to do is to can noscroll function when the user will open navbar

function noscroll() {
  window.moveTo(0, 0);
}

function menutoggle() {
  if (menuItems.style.maxHeight == '0%') {
    menuItems.style.maxHeight = '30%';
    window.addEventListener('scroll', noscroll);
  } else {
    menuItems.style.maxHeight = '0%';
    // How can I unable to scroll here?
  }
}

Upvotes: 2

Views: 471

Answers (3)

Bora
Bora

Reputation: 1522

This would work I suppose:

function noScroll() {
  window.scrollTo(0, 0)
}

window.addEventListener('scroll', noScroll)

Upvotes: 1

pradeexsu
pradeexsu

Reputation: 1145

Setting the value to scroll, the overflow is clipped and a scrollbar is added to scroll inside the box. Note that this will add a scrollbar both horizontally and vertically (even if you do not need it):

overflow: scroll;

use of overflow

Upvotes: 0

Adiat Hasan
Adiat Hasan

Reputation: 411

one solution can be: while nav is open wrap it with another div and then make that div position absolute. Hence it will be outside of the document flow. then give it a height of 100vh width 100vw. After that place, your main nav bar as you like inside the parent div.

Upvotes: 1

Related Questions