Stop iPhone body scrolling when fullscreen overlay is opened

My goal is to prevent the body from scrolling when my fullscreen overlay navigation is open. I have added a class show-nav to the body with the CSS attribute overflow: hidden, which works perfectly on desktop but not on iPhones.

After researching similar issues on stackoverflow, I attempted using e.preventDefault(), which did stop the scrolling but then kept the body unscrollable even after closing the navigation menu. How can I allow scrolling again when the navigation menu is closed?

    $( "button.navbar-toggle" ).on( "click", function(e) {
     $('body').on('touchmove', function (e) {
              e.preventDefault();
     });
      $('html').toggleClass('show-nav');
      $('body').toggleClass('show-nav');
    });

Answer №1

Appreciation to @Tim for leading me in the right direction: here's the solution that worked for my situation:

    // Toggling the show-nav class on html/body when nav opens (preventing scroll on desktop) and disabling scroll on mobile
    $( "button.navbar-toggle#open-nav" ).on( "click", function(e) {
      $('body').bind('touchmove', function(e){e.preventDefault()});
      $('html').toggleClass('show-nav');
      $('body').toggleClass('show-nav');
    });
    // Toggling the show-nav class on html/body when nav closes (preventing scroll on desktop) and enabling scroll on mobile        
    $( "button.navbar-toggle#close-nav" ).on( "click", function(e) {
      $('body').unbind('touchmove');
      $('html').toggleClass('show-nav');
      $('body').toggleClass('show-nav');
    });
    // Clicking any link will close the nav menu and enable scrolling on mobile
    $('.site-nav ul.nav li').on("click", function(e) {
      $('body').unbind('touchmove');
      $('html').toggleClass('show-nav');
      $('body').toggleClass('show-nav');
    });

Hoping this solution can benefit others dealing with a similar issue :) Cheers!

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

Showing JSON data as a table in an HTML format

After receiving the JSON String from the server as a response, which can be viewed here, I've implemented the following Jquery Code: function loadCategories() { $.ajax({ type: "POST", url: "/Services/ControllerService. ...

Running the NPM build command results in an error specifically related to an HTML file

I encountered an issue in my AngularJS application when running the command: npm run build -- -prod The error message I received was: ERROR in ng:///home/directoryling/appname-play.component.html (173,41): The left-hand side of an arithmetic operation ...

Specify the location of the resize button (corner)

Scenario : At the bottom of the page, there is a resizable textarea, However, having the resize button at the bottom-right corner does not provide the best user experience (try it out), Issue : I am wondering if there is a way to move the resize butt ...

A mobile phone screen cannot be fully filled by an image

I'm working on a way for an image to cover the entire screen of a mobile phone. However, when I preview it on an iPhone through a laptop, the image only covers a small portion of the iPhone's screen rather than filling it completely. Below is the ...

PHP-generated HTML onclick attribute malfunctioning

Here is the PHP code I am currently working with: $listing .= "<button onclick='updateValue(".$id.", ".$key.")'>change value</button>"; The variable $id is an integer and functions correctly. However, $key is a reference from a for ...

Javascript error in formatting

Currently, I am utilizing an inner join operation on three tables and displaying the resulting table using XML. <SQLInformation> <Table>tblechecklistprogramworkpackagexref prwpxref</Table> <TableJoins> INNER JOI ...

The image displayed on the HTML scrset attribute is not the correct one

I'm experiencing an issue with correctly loading the responsive image using scrset. The sizes attribute I am using is: sizes="(max-width: 767px) 95vw, 768px" and for the srcset attribute: srcset="https://cdn.runningshoesguru.com/wp-content/uploads ...

Utilizing JQuery and AJAX to fetch a specific variable

Hey there, I'm new to jQuery and AJAX. I've been attempting to pass a value from page 2 to page 1 using this script, but it doesn't seem to be working properly. Check out the script: function prova() { var parametro = $("#nome_priv ...

Guide to making a leaf node with jstree version 3.0.0

The tree functionality seems to be functioning correctly, however, I am facing an issue with creating a leaf file. Although it is able to create a folder file, the creation of a leaf file seems to be problematic. Below is the code snippet that I am strugg ...

Tips on choosing just the selected checkbox values

In my CodeIgniter view, I am utilizing AJAX to post data to my controller. <script type="text/javascript"> $(document).ready(function(){ // find the input fields and apply the time select to them. $('#sample1 inp ...

Flexbox grid implementation for a stylish Bootstrap 4 masonry layout

Can a masonry column layout be achieved using the flexbox grid provided by Bootstrap 4? It appears that all the columns have the same height. ...

VueJS Components experiencing issues with displaying images

Recently, I delved into learning VueJS and successfully created a basic restaurant menu page all within a single file. Excited by my progress, I decided to refactor the project using vue-cli, but hit a snag with the images not displaying properly. The cur ...

Manipulate the text within a canvas element using JavaScript

In this scenario, suppose json.mes represents the received message from Ajax. There is a button implemented to display the message in a canvas box. Although I have attempted to retrieve the message using getElementById (as shown below), it seems to be in ...

The issue with IE rendering shadows and how it relates to jQuery

Recently, I attempted to use jQuery to create a "fade in" effect on a section of my website. However, when testing it in IE8, I encountered an unexpected issue. While the box successfully faded in, the shadow that appeared during the transition displayed a ...

Is there a way to reverse the animation playback in Angular?

I am working on an animation that involves a box fading from its original color to yellow. However, I would like to achieve the opposite effect: when the page loads, I want the box to start off as yellow and then fade back to its original color. The challe ...

Warning: The codesign verification for the application has failed

While trying to run my app on my iPad today, I encountered a warning in XCode. The error message stated: Application failed codesign verification. The signature was invalid, contains disallowed entitlements, or it was not signed with an iPhone Distribut ...

Invoke a jQuery function in the parent page using regular JavaScript from an iframe

I have successfully created an iframe using regular javascript. Inside the iframe is a Jquery function along with JQuery itself and it functions correctly within the iframe. However, I am now looking to execute this function from the parent page instead of ...

Achieving a horizontal alignment of these four div elements containing images

I need some assistance with aligning the divs using the .acontainer class. It was working perfectly before adding the images, but now it's all messed up. I've tried various adjustments and searches online, but I'm still stuck. Thank you for ...

Steps to ensure that a particular tab is opened when the button is clicked from a different page

When I have 3 tabs on the register.html page, and try to click a button from index.html, I want the respective tab to be displayed. Register.html <ul class="nav nav-tabs nav-justified" id="myTab" role="tablist"> <l ...

PHP is causing a mysterious slash to continuously pop up within a string during the creation of session data

I encountered an issue while using session data to set the value of an event title. When the title contains an apostrophe, such as "Britain's Digital Future," a backslash keeps appearing before the apostrophe. This results in multiple slashes being ad ...