Ways to prevent body scrolling when the mobile menu is open

Is there a way to prevent the body from scrolling when I open a menu on a mobile device?

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

Answer №1

Please include the following CSS in your stylesheet:

body.onScroll {
  overflow:hidden;
}

Answer №2

It appears that you are interested in displaying a toggle button exclusively on mobile screens. To achieve this, you can implement the following code snippet:

Access JSFiddle Example

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;
}

By clicking on the toggler, the body will receive a dynamic class that prevents scrolling.

Feel free to reach out if you require further assistance.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The incompatible peer dependency of node.js is causing issues with running npm update

C:\Users\Projects\webapp +-- UNMET PEER DEPENDENCY <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6012050103142051554e544e52">[email protected]</a> `-- UNMET PEER DEPENDENCY <a href="/cdn-cgi/l/e ...

Navigating Overlays with Python and Selenium

driver.find_element_by_class_name("lnkClassInfo").click() time.sleep(2) element = driver.find_element_by_css_selector("#popup input[value='BOOK THIS CLASS NOW']") driver.execute_script("arguments[0].click();", element) ERROR: selenium.common.exc ...

I am having trouble accessing the div. What could be causing this issue?

Here is the code I have written: - <html> <head> <title>Pranav Sharma</title> <style> @import url(http://fonts.googleapis.com/css?family=Lato:300,400,700); body { background: #ecf0f1; font-fami ...

Exploring the Logic Behind My State Store Layout - Comparing User Interface to Application State

I'm starting to get a bit confused about the difference between application state and UI state. Whenever a user picks an activity from a list on the page, an API call is triggered to retrieve the announcements for that specific activity. Is the id of ...

Shining animation of a quote in a right angle using CSS3 and/or Javascript

I have a vision for an animation that involves three successive right angle quotes, like » » », glowing one after the other from left to right and then back again. This could be a unique effect that developers might find interesting and v ...

Navigate directly to a designated element in a React component without the need to scroll when clicking a link

When viewing a user's profile in React, clicking on an image currently scrolls to that specific image within the entire images page. However, I am looking to modify this behavior so that it navigates directly to the image element without any scrolling ...

The call function in Tween.js fails to execute after adding an EventListener

I encountered an issue while using tween.0.6.2. The following code snippet (borrowed from the tween.js Getting Started page and slightly simplified) works perfectly: createjs.Tween.get(circle) .to({x: 400}, 1000, createjs.Ease.getPowInOut ...

Using Jquery to dynamically set value for select form input

I have almost completed a webshop with a product catalog page featuring multiple products, as well as a product information/overview page focusing on a single product. The product catalog page currently includes a "read more" button that redirects to the ...

Incorporating external files into Javascript code may cause issues with its functionality

Currently, I have a fully developed PHP theme that I am in the process of designing. Within this theme, I have integrated an image slideshow plugin to enhance its functionality. The following code represents the implementation of the image slideshow: &l ...

"Highcharts malfunctioning in Internet Explorer when loading the page through AJAX - Issue with jQuery throws an exception

I am currently facing an issue with IE while loading a page via AJAX. Our technology stack includes jQuery, Bootstrap, Select2, and Highcharts. I suspect that the problem may be related to the Select2 ajax call, but the no-results code appears to be workin ...

Using arrow functions in node.js

Can someone help me understand how arrow functions work? I know that arrow functions are typed like this () =>, but I'm curious about how the function that contains the arrow function knows how to call it. For example: app.listen(3000 , () => ...

Receiving an 'undefined' result in an asynchronous response even with 'await' and 'then' statements implemented

I'm struggling with sending a GET request, parsing the response, and passing it to another function. It seems like I'm having difficulty dealing with the asynchronous nature of the response. I prefer to stick to using Node.js' built-in modu ...

AngularJS $routeProvider not working as expected

Today marks the beginning of my journey with AngularJS, and I'm facing a roadblock in the "routing" aspect. I've successfully created a controller and a view (refer below), but upon attempting to run it on my local server, an error pops up: Unca ...

I'm looking to replicate the testimonial slider from the link provided. How can I achieve this?

I'm looking to replicate the testimonial section layout seen on Kanbanize, which features both vertical and horizontal sliders. I'm unsure of how to link the two sliders so they move in sync. I typically use Swiper for carousel/slider functionali ...

Adjust the width of the list items based on the specified condition

I need assistance with adjusting the width of items in a list. The list includes: <ul> <li>One</li> <li>One-One</li> <li>Two</li> <li>Two-Two</li> </ul> The display should be dyn ...

Understanding the application of JSON data can be simplified by following these

I am looking to manipulate dates by either adding or subtracting them, but I am unsure of how to extract the dates from the other data present. var fetch = require('node-fetch'); fetch('https://api.nasa.gov/planetary/earth/assets?lon=100.7 ...

Obtaining and Assigning Filter Values in Material Table

Is there a way to programmatically obtain and adjust filter values with material-table? I am looking to enable users to save filter settings as reports and access them whenever necessary. ...

An Unexpected Token Leads to a SyntaxError in Jest's CSS-Modules

I have successfully configured my jest to allow the usage of static files by following their detailed documentation. However, despite implementing the instructions correctly, I am still encountering an error: What steps can I take to resolve this issue an ...

angularJS transformRequest for a specified API call

Looking for a way to pass multipart/formdata through a $resource in Angular so I can post an image. I discovered a helpful solution on this Stack Overflow thread. However, the solution applies to all requests within my modules, and I only want it to apply ...

jQuery Typeahead implementation malfunctioning when text matches either the first or last character

It seems like there is a problem with the .split() method, especially when the characters match the beginning or end of a name. To see the issue in action, visit this JSFiddle link: http://jsfiddle.net/5uebxvex/ I'm working with the Employees tabl ...