Stop the Body Element from Scrolling on Touch Screen Devices

Issue with iOS Homescreen Web Application:

My web application on the iOS homescreen works flawlessly, but I am facing a problem with bounce scrolling affecting fixed elements and site-related animations. I have managed to make inner div elements perform touch scrolling events with momentum and bounce style in iOS, which works perfectly. However, the bounce scrolling is causing issues with certain elements on the page.

When I attempted the following solution:

document.ontouchmove = function(e) {e.preventDefault()};

The issue of bounce scrolling stopped, but now the entire application became unscrollable.

What I'm Looking For:

I want the body to remain completely locked in place. Users should be able to scroll within certain designated areas without affecting the rest of the page. The content area should scroll smoothly without any bounce effect on the body or other fixed elements. Essentially, I need a scrolling content area with no scroll capability elsewhere on the page.

Summary: Body bounces during scroll. Seeking scrolling functionality within specific areas only, while keeping the body stationary.

In my research, I have come across several popular solutions for similar issues:

1 2 3

I wanted to mention this before anyone assumed I had not tried searching for a solution. Despite spending hours exploring different options, I wanted to highlight the most commonly recommended ones to avoid duplication of questions.

Answer №1

A few days ago, I successfully solved a problem and created a helpful jsbin demonstration to showcase my solution:

Check out My Working jsbin Example

When viewing this link on an iPad, you'll notice that the text is scrollable. Feel free to interact with the screen when there's no touchmove event happening.

If you explore further, you'll observe that only the inner textfield moves as intended. This behavior is achieved by placing my .scrollable class within the .container class, where the former occupies the full height of its parent container.

Now, increase the height of the container to something like height: 500px. The objective is to eliminate overflow yet maintain sufficient whitespace on the iPad display. Attempt to scroll or drag it... No touchmove events will be triggered, and the screen will remain still.

My JS script identifies whether an object has overflow after being touched. If an overflow exists, it enables scrolling; if not, it avoids sending a scroll event.

Feel free to experiment with it and provide feedback if you need additional examples or encounter any issues. Currently, the only known issue occurs when aggressively interacting with the site during a touchmove event or while the page is loading. While I don't consider these as "bugs," I'm open to suggestions for addressing them!

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

Using assert.rejects() in Mocha and Node: A Complete Guide

Following the instructions in the documentation, I attempted to use assert.rejects to test for an error message (I have Node version above v10). However, no matter what message I specify, the test always passes. What could be causing this issue? it("is f ...

Toggle table column visibility in AngularJS by using a dropdown selection in the first column

Implementing angularjs I have a situation where I have a table with two columns. The first column is linked to an array, while the second column contains a dropdown menu. My goal is to display or hide 5-6 additional columns (containing text and dropdowns ...

Error encountered while building in Xcode 7 beta 4: com.apple.CoreSimulator.SimRuntime.iOS-9-0

The issue that arises when building a previously functional project is perplexing. Images.xcassets: Failed to locate an appropriate device for the specified SimDeviceType : com.apple.CoreSimulator.SimDeviceType.iPad-2 with runtime SimRuntime : 9.0 (13A430 ...

Using a React button to sort through an array

Hey there, I'm currently working on an app that filters a list based on user input. The idea is to click on buttons to exclude users with specific letters in their names. However, the code I have right now isn't functioning properly. Any assistan ...

Adjust the field's color depending on the outcome displayed within

I'm trying to implement a feature where the field value changes to green when "OK" is selected and red when "NOK" is chosen. I have written the following JavaScript code but it's not working as expected - the colors change before clicking submit, ...

What is the solution to resolving the warning about the router no longer defaulting the history prop to hash history?

I have implemented browser history in my code within routes.js export default ( <Router history={browserHistory}> <Route path="/" component={Main}> <Route path="home/:username" component={Home}> <IndexRoute co ...

Which is better for testing in Cypress: classes or functions?

When it comes to testing in Cypress, which approach do you believe is more efficient? Functions: 'support/pages/login.js' export const login = (username, password) => { cy.get('#username').type(username); cy.get(& ...

PrimeNG ToolTip fails to appear

I am experiencing issues with getting tooltips to display properly on my webpage. When I hover over the tooltip trigger, the screen seems to expand with a vertical scrollbar appearing, but no tooltip is shown. It appears as though the tooltip might be tryi ...

What could be causing my Vue.js sorting array script to malfunction?

I'm encountering an issue with sorting the table by Date. The sort function used to determine the type of sorting no longer works, and I'm unsure why. html: <th @click = "sort('data_produktu')" class="date">Da ...

Create a dynamic table using d3.js that incorporates JSON data with interconnected information and hyperlinks

I am currently facing a coding challenge without any specific example to refer to. I have a JSON data retrieved from the server side and need to display it as a table on the client side. While using d3.js for this task seems feasible, the complication aris ...

Issue with interaction between jQuery AJAX and PHP login functionality

Currently, I am attempting to implement an inline login feature that triggers whenever the PHP $_SESSION['logged_in'] variable is not defined (this variable gets set when a user logs in). The challenge arises when I try to keep the user on the sa ...

How can I efficiently remove elements from the end of an array in Vue.js?

Is there a way to splice data starting from the back with a higher index? When clicking on the .delete div, how can I make it so the .image-box div deletes starting from the end? I need help implementing this feature in my code. Here is a snippet: < ...

Illuminate the tag with the touch of your finger

Initialization: I have a collection of 5 labels, each tagged from 1 to 5, connected to an IBOutletCollection set up as @property (nonatomic, retain) IBOutletCollection(UILabel) NSMutableSet* myLineCollection; My Objective: As I move my finger across ...

Trouble fetching the concealed field data within the MVC framework

Upon executing the code provided below on an ASP.NET web form, I noticed that the value of the hidden field customerDeviceIdReferenceCode appears in the page source. <div id="customerDeviceIdReferenceCode" style="display:none;"> <inpu ...

The function in jQuery that can be used to hide a <div> when clicked

I am facing a problem with my jQuery code that is supposed to remove/hide a specific div. Despite checking various examples, I can't seem to make it work properly. This issue arises in the context of jQuery on Drupal 7. The live site where this proble ...

Display loader until the document body has finished loading

Many developers have shared solutions for displaying a loader while an element is being loaded. However, my challenge is unique. I want to display a loader before the document body is fully loaded. The issue arises from an AJAX call in my code: var url = ...

What steps are needed to transform an HTML string into functional HTML code that will be visible on the front end of a website?

When using a v-for loop to iterate through items, the {{ item.data }} contains HTML elements with defined values. For example: The value of {{ item.data }} is a string "<'qr-code value="this has specific infos that is already created for this spec ...

Best practices for linking Websocket events to refresh my React Component

Although my main focus is C++ backend development, I've been dabbling in learning React on the side. I have written a simple file to utilize websockets. import React, { useState } from "react"; var serverMessage = ""; var webSocke ...

What communication method would be best for ensuring scalability if I were to develop an AJAX chat application?

A while ago, I created an AJAX chat using ASP.NET MVC and jQuery. The functionality involved the javascript hitting the server every 7 seconds to check for new messages. As the chat grew in users, this method became a performance concern due to the increas ...

The callback function in AngularJS' $http is failing to trigger

$scope.submitNewUser = function() { $http({ method: 'POST', url: 'api/user/signup', data: {'user': $scope.user}, headers: {'Content-Type': ...