Tips for identifying when you've reached the bottom of a mobile overflow while scrolling rapidly

When scrolling on mobile, you can initiate a "pull" motion where the content continues to move on its own. How can one determine when this movement has ceased? The ontouchmove event does not trigger during this autonomous scrolling.

For example, I want to remove the "more content" banner once the user has scrolled all the way to the left. I attempt to track how much scrollable area remains using the ontouchmove event. However, since this event only registers when the user actively touches and moves the content, it's impossible to gauge the remaining scrollable area.

If I manually scroll to the end by pressing down on the content, everything works smoothly:

https://i.sstatic.net/3sXkp.gif

However, if I rapidly pull the content and it continues to scroll autonomously, the ontouchmove event fails to activate upon reaching the end, thus making it challenging to detect that the terminal point has been reached:

https://i.sstatic.net/IOEV7.gif

function onTouchMove() {
  const node = document.getElementById("scroll-container");
  const overflownOnRight = Math.ceil(node.scrollLeft + node.offsetWidth) < node.scrollWidth;
  if (!overflownOnRight) {
    document.getElementById("info-banner").style.display = "none";
  } else {
    document.getElementById("info-banner").style.display = "block";
  }
}
.scroll-container {
  margin-top: 100px;
  width: 100%;
  height: 40px;
  overflow-y: auto;

  &::-webkit-scrollbar {
    height: 0;
    width: 0;
    display: none;
  }
}

.reference-point {
  height: 30px;
  width: 100%;
  border: 5px dotted pink;
}

.scrollable {
  width: 1200px;
  height: 100%;
  background: blue;
  display: flex;
}
<div id="scroll-container" ontouchmove="onTouchMove(event)" class="scroll-container shadowed">
  <div class="scrollable">
    <div class="reference-point">
    </div>
  </div>
</div>

<div id="info-banner">
  Scroll Right For More Content
</div>

Answer №1

If you want to detect when scrolling has stopped, one option is to utilize the JavaScript event scrollend. This feature is still relatively new and may not be supported in all browsers. You can find more information about it here and here. Alternatively, for browsers that do not support scrollend, you can consider using a polyfill like scrollend polyfill.

Another approach that is less reliant on browser support is to continuously check for the scroll event.

To implement the scroll event effectively, it's recommended to use a debounce function to handle the frequent occurrence of scroll events while the user is scrolling.

Within the debounce function, you can then verify if the user has reached the end of the scrollable container.

Here's a code example:

function debounce(func, wait, immediate) {
    var timeout;
    return function() {
        var context = this, args = arguments;
        var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};

function handleScroll() {
    const node = document.getElementById("scroll-container");
    const overflownOnRight = Math.ceil(node.scrollLeft + node.offsetWidth) < node.scrollWidth;

    if (!overflownOnRight) {
        document.getElementById("info-banner").style.display = "none";
    } else {
        document.getElementById("info-banner").style.display = "block";
    }
}

var debouncedHandleScroll = debounce(handleScroll, 250);

document.getElementById("scroll-container").addEventListener('scroll', debouncedHandleScroll);

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

tips for transferring a variable to a class function

I'm working on a JavaScript object that looks like this: var myObject = { initialize: function() { this.property1 = 'value 1', this.property2 = 'value 2' }, outer: { inner: { inner_ ...

NodeJS API integration failure during ReactJs login attempt

Currently, I am tackling a small project on implementing user login functionality in ReactJs with Nodejs and Express-session. The issue I am facing is that my front end (React login page) isn't functioning properly. Below is the Fetch API code snippet ...

Display a series of elements in rows using styled-components

Here is a simple array that I need help rendering into a grid structure. Currently, the array is displayed like this: HAMDDN I want each element to be on a new row like so: HA MD DN Any suggestions on how to achieve this? Below is the full code. I am ...

There are currently no articles found that match the search query

Recently, I started working with Django, but I am facing an issue with slug. Whenever I send a request to the Slug, I encounter an error. The error message I receive is: Articles matching query does not exist. Furthermore, I am facing another error while ...

In ReactJS, when you encounter TextField values that are "undefined", it is important to handle them appropriately

I'm a beginner when it comes to ReactJs and Material-UI, so please bear with me if my question seems silly. Currently, I have a file named Main.js which includes the following code snippet: handleChange = (name, event) => { if(event==null) ...

Every time I refresh the page, my table is getting filled with blank rows

I created a simple example featuring a form and some PHP/JavaScript code. The JavaScript is used for form validation, while the PHP is utilized to update a MySQL table. function checkForm(){ var x = document.forms['form1']['first'] ...

adjust division size proportionally to another one

I am trying to create a layout with right and left divisions where the size of the left division matches the height of the browser window (100vh). The left division consists of two sub-divisions that need to have variable sizes but collectively match the ...

Arranging information extracted from an XML document following an ajax request

Here is a snippet of XML data sample to work with: <?xml version="1.0" encoding="ISO-8859-1"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>U ...

Adjust the background shade of specific characters within an input text field

When a user inputs a string into a standard HTML input field (type="text"), such as "2013/13/29", and validation reveals that the number 13 is invalid in this context, I would like to visually highlight it by changing its background color to red while tu ...

Running a Python Selenium script

I need some help executing this script using selenium. <div class="vbseo_liked"> <a href="http://www.jamiiforums.com/member.php?u=8355" rel="nofollow">Nyaralego</a> , <a href="http://www.jamiiforums.com/member.php?u=8870" rel="nofollo ...

Has the web application continued to run in the background even after toggling between tabs on the browser?

Does the app continue running in the background even when I'm not on that specific tab? Or does it pause when I switch between tabs, requiring a new request to the server for any updated data from the database upon returning to that tab? Edit: Curren ...

Customize the appearance of alert boxes in ajax requests

Is there a way to customize the default ajax alert box? I currently have code that deletes a user and reloads the current page. It functions properly, but I want to enhance the styling of the alert box that appears before the user is deleted. function d ...

CarouFredSel Transition Troubles

I am currently using CarouFredSel to create an image carousel, but I am encountering some issues with the transitions between items. My goal is to incorporate simple HTML elements into the carousel instead of just plain images. However, when I attempt to ...

What is the best way to format a list <li> inline within PHP code?

Is there a way to display this list horizontally? Using list-inline makes the list horizontal, but it doesn't style the <li> elements. I would like to apply a specific class to the li elements, but I'm unsure where in the code to add the s ...

The local webpage varies from the online version

After uploading my website to my hosting space, I noticed that the online version looks different from the local version. In my local version, I am debugging with Dreamweaver and checking responsive behavior with Google DevTools. Everything seems fine - ...

Setting default values for dropdowns in ng-repeat using AngularJS

I am working with an array of objects that have three properties: attorneyId, attorneyName, and attorneyList. The attorneyList should be displayed as a dropdown menu. If both attorneyId = null and attorneyName = null, then the dropdown will only show Choos ...

The Child Component in React Native Always Shows Undefined Response Status from API

In the code snippet below, I am facing an issue where the status returned by an API call is always showing as undefined in the child component, even though I can see the expected status code when logging it in the parent component. It seems like the child ...

Execute a single test from a particular test suite using Jest

Within my file named "test-file-1", I have several describes (test suites) with distinct names, each containing tests that may share similar names across different test suites. In order to run a single test suite or test, I enter the following command: n ...

Is it possible to have identical Plotly-Dash Python code yield different outcomes, with one version functioning properly while the other does not?

When running this Plotly Dash in Jupyter Notebook with Firefox, I encountered an interesting problem. Manually typing out the code didn't work, but when I copied it from a script provided by my course, it worked perfectly. The code and formatting were ...

The issue of background and footer distortion arises in PhoneGap when the keyboard is opened

Currently, I am experiencing screen problems with phonegap. The issue arises when a keyboard is opened, causing the back button at the bottom of the page to move above the keyboard and resulting in the background image appearing shorter. How can this be re ...