1

How can I stop the body scrolling if I open a menu on a mobile device?

function bodyScrollStop() {
  $('.header .navbar .navbar-toggler i').on('click', function (event) {
    $('body').toggleClass("onScroll");
  });
}
bodyScrollStop();

2 Answers 2

1

Plz add this in css..

body.onScroll {
  overflow:hidden;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for give me solution but I actually want when i click on navigation menu icon on mobile screen
ok no pbs -:) You can use whatever you want.. but you can add the class when click the button.
0

Well i believe that you are showing that toggle button on mobile screens only. Then you can try below code.

JS Fiddle Link

JS Code:

function bodyScrollStop() {
  $('.header .navbar .navbar-toggler i').on('click', function (event) {
    $('body').toggleClass("no-scroll");
  });
}
bodyScrollStop();

CSS:

i {
  background: green;
  height: 44px;
  width: 44px;
  display: inline-block;
  color: #fff;
}

.no-scroll {
  overflow: hidden;
}

Now if you click on that toggler body will get dynamic class and will prevent it from scrolling.

Let me know if you need more help.

Comments

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.