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

"Which is better to use with jQuery: the .on method for handling

//creating toolbar elements dynamically var $toolbar = $(".toolbar"); $.each(tools, function (i, tool) { $("<img>", tool).appendTo($toolbar); }); var $tools = $toolbar.find("img"); //defining drag and drop functionality $toolbar.on("dragstart", ...

Challenges with managing jQuery's .blur() and .focus() events

I am encountering an issue with a form I have created that contains several input fields. When a user clicks on any of the input fields, a hidden div appears with various options related to that field. The user can then select an option from the div, which ...

What is the best way to exchange configuration data among Javascript, Python, and CSS within my Django application?

How can I efficiently configure Javascript, Django templates, Python code, and CSS that all rely on the same data? For example, let's say I have a browser-side entry widget written in Javascript that interacts with an embedded Java app. Once the user ...

Tips for altering the color of an activated Bootstrap dropdown toggle

When the Dropdown menu is open, I would like to customize its default colors. Specifically, I want to change the border color and background using CSS. Below is the HTML code snippet: <div class="row menu"> <ul class="nav nav-pills pull-right"& ...

jQuery Ajax is failing to transmit data in a POST request

I am encountering an issue with an Ajax request involving posting data using jQuery. Previously, everything worked fine with the GET method, but now that I have a large amount of data to send, I am receiving a (Request-URI Too Long) error. As a result, I a ...

Error on JSP Hello Page

As a novice in JSP, I am attempting to create a simple JSP page where I can set my class fields for name and surname and display them on the page. Below is my Java class: package org.mypackage.person; /** * * @author cemalinanc */ public class Person ...

Is there a way to divide text in half while preserving the HTML structure?

I am in need of setting up an RSS feed where only a portion (or a specified number of characters) of the article's text is displayed, while keeping all HTML tags intact (such as images or YouTube videos). For instance, what should happen if the chara ...

Content and visual side by side

My logo and header image are giving me trouble. Whenever I attempt to center the header image, it keeps moving to the next row. How can I make sure it stays on the same row? Check out our website Thank you ...

add before the beginning and after the end

Hi there, I'm trying to figure out how to add before my initial variable and after my last one. This is the code snippet: $pagerT.find('span.page-number:first').append($previousT); $pagerT.find('span.page-number:last').append($n ...

Develop a dynamic animation using gradient and opacity properties

I'm still getting the hang of HTML, JavaScript, and CSS but I recently made some changes to someone else's code to animate a gradient. The original code used background: rgb, but I switched it to background: rgba. It seems to be working fine, but ...

The Splitter remains inactive until a peculiar series of actions is taken

Trying to troubleshoot this issue with a library called Split.js, which can be found here: https://github.com/nathancahill/Split.js I've encountered an interesting problem where I have to disable the height CSS property of my container, move the spli ...

Dynamic jQuery menu with sliding animation

Currently, I am in the process of developing a jQuery dropdown menu and things are going well so far. The only issue I am facing is that once a folder or tree is opened, there doesn't seem to be a way to close it again. I have a hunch that using EQ or ...

"Receiving a rendered HTML page instead of an SQL result from a jQuery POST request

Currently, I am utilizing codeigniter within my application and have implemented a jQuery post function that is performing flawlessly. $.post('getticketstacktype', {ticketnumber:ticketnumber},function(result) { alert(result); } Subsequently, I ...

Retrieve the height of a div element in an AngularJS framework, and assign this value to a corresponding attribute of another element

Is it possible to retrieve the height of an element using AngularJS and assign it to another div's attribute? In my Bootstrap website, I have a fixed sidebar that needs to stop before reaching the footer. To achieve this, I currently have to manually ...

Solve the problem with SCSS at the component level in NextJS

I've decided to transition my regular React app to Next.js. In the past, I would simply import SCSS files using: import from '.componentName.scss' However, now I need to import them using: import style from 'componentName.module.scss ...

Retrieve the text content of a datalist option by accessing the label with jQuery

Utilizing data from a Json, I am populating a data-list in html. The options are added to the data-list with both value and label text. Upon clicking an option, I aim to insert both the value and text into a form text field. While accessing the option&apo ...

Prevent unnecessary image resizing during parallax scrolling animation

Check out my demonstration where the image scaling results in the margin-top of the parallax wrapper being unable to dynamically adjust. Demonstration: http://jsfiddle.net/KsdeX/12/ .wrapper-parallax { margin-top: 150px; margin-bottom: 60px; } W ...

Menu Drop on top of Flash player

A challenge I am currently facing is with my live event site and the list of events displayed in a mouseover menu. The issue arises when the flash streaming player remains in front of the mouseover menu, causing interference across all versions of Interne ...

Find the line containing the selected text within a JavaScript code

I am working on a contentEditable div where users can enter multi-line text. I need to be able to inspect the line that the user is currently typing in when they press enter. Is there a way to retrieve the context of that specific line (or all lines)? Is ...

How to position two divs in a row using Bootstrap

Looking at the output below: https://i.stack.imgur.com/pQM8F.png The blue circle is placed in one div, while the text is in another. I'm perplexed by why the first div appears on top and the second doesn't seem to align properly. Here's th ...