Displaying results as you scroll down within a container

I want to create a feature similar to Facebook where more results are loaded when the page is scrolled down. I need to style both an outer and inner div for this implementation.

  .Outer{
      height: 1430px;
      margin-top: -12px;
      overflow: hidden;
      width: 99%;
    }
    .inner {
      height: 1435px;
      overflow-y: scroll;
      position: relative;
      width: 103%;
    }

To achieve this, I have two div elements named Inner and Outer. I have added an inline onscroll function to the inner div. Currently, I am able to scroll through records inside the Inner div without showing a scrollbar, with 10 records loaded initially. When I reach the 7th record while scrolling, I want to make a server call to fetch 10 more results and append them to the Inner div.

What is the best method to determine the scroll position at the 7th record and initiate a server action?

Considering that the website needs to be responsive, will the onscroll function work effectively on all devices? Are there any better approaches to meet this requirement?

Answer №1

I encountered a similar issue in the past.

Currently, I am using the following solution specifically tailored for Chrome:

var handleScroll = function(event) {

        var container = event.target;
        if ((container.scrollTop + container.offsetHeight) 
             + 1 >= container.scrollHeight) { 
             ...
        }
}

The main focus is on detecting when the user scrolls to the bottom without concerning yourself with the 7th record.

Furthermore, you can consider implementing these additional features:

  • Include a small animated image (such as a rotating circle) to indicate server data retrieval. This can be achieved by inserting an extra div at the top or bottom of your records based on the scroll direction, and removing it upon completion/error of the server call.
  • Be cautious about excessive onscroll events leading to frequent server calls. To prevent this, you can implement throttling/debouncing using a timer:

    document.getElementById('myid').onscroll = function(oEvent) {
        if (this.timer) {
            clearTimeout(this.timer);
        }
    
        this.timer = setTimeout(function() {
            this.handleScroll(oEvent);
        }.bind(this), 250);
    }.bind(this);
    

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

Troubleshooting problem with AJAX returning JSON values

Utilizing a jQuery post request in the following manner: $.post('url', {data: some_data}, function(data, textStatus, jqXHR) { console.log(data); //for debugging console.log(data.status == "ok"); //for debugging .... }); The url hits ...

Using (javascript:) within Href attributes

Recently, I've noticed some people including "javascript:" in the href attribute of an a tag. My question is: what is the purpose of this? Does it guarantee that clicking on the a tag directs the function of the click to JavaScript for handling, rathe ...

Change the CSS of a selector using jQuery before it is triggered

I am attempting to manipulate the CSS :before and :after selectors using jQuery. Despite trying various methods, it appears that the DOM is unable to access these CSS selectors. My goal is to dynamically change the background-color of an element. For inst ...

What strategies can I implement to minimize repetition in React when utilizing the useState hook?

I have implemented useState in my React Native application. I defined states named lstandtime and lacttime. Additionally, I have created functions called onSubmitfunc, LsonChangefunc, and LaconChangefunc. We initially required 3 instances of the MainCons ...

Can AJAX be utilized to update a single attribute of a model?

I have a model called JobApplicationForm with two parts - a short application and a long application. The first page posts certain attributes to the model, and once the last 3 pages are completed, another post updates the model to be fully filled out. publ ...

The labels displayed on ChartJs are inaccurate

As a student who has been learning programming for about a month, I must admit that there may be many mistakes in my code. In developing a website, I have incorporated a chart from the ChartJs library. The chart consists of an outer circle representing ho ...

Steps for showing retrieved data individually:

Currently, I am utilizing angular-js, JavaScript, and HTML in my project. I have successfully retrieved a list of customer names and their corresponding details from the database. However, I am looking to implement a feature where each record is displayed ...

Toggle the visibility of a table column based on user selection in Vue.js

I am having issues with displaying and hiding based on checkbox click events. Can anyone assist in identifying the mistake? When clicking on an ID, it should hide the ID column. Similarly, clicking on "first" should show/hide based on checkbox clicks, and ...

Selecting the perfect default font using font-display: fallback

When I use a particular font and find that its loading time is too high, I want to set a default font. So I experimented with the following code: font-display: fallback; It seems to be working fine (although I haven't checked compatibilities yet), ...

Is there an Angular Profile service offering getter and setter properties?

Can a singleton Angular service be created with getters and setters along with logic implementation? I was provided with the following code snippet and tasked with replicating it in an Angular service. Although it may seem straightforward, I'm finding ...

What impact does changing the Device Language have on a heading?

At the top of an html page, I have the word "Color" in a heading. If a user's device is set to British English, I would like the text to automatically switch to "Colour". What is the best way to accomplish this with minimal Javascript? ...

ajax used to retrieve xml data and process it

I've been working on retrieving information from an API (specifically BBB) and the response I'm getting looks like this: <?xml version="1.0"?> <response> <returncode>SUCCESS</returncode> <meetingID>ldapreade ...

Capturing JSON response in Jquery

I'm struggling to figure out what's going wrong with this code. I can't seem to get any response in the status field... <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"> </script> <input typ ...

Tips for showcasing information on an HTML website

I am currently facing an issue with displaying addresses retrieved from a database in my project. The problem lies in the formatting of the address when it is displayed on the page. Currently, the address is being displayed as follows: Address: 1 Kings S ...

Selenium WebDriver is having trouble locating an element

Currently, I am employing the latest version of selenium webdriver. The issue arises when Selenium fails to locate an element (specifically an input field). Here is the relevant HTML code: <input id="findPath" class="pathCom" type="text" style="width: ...

The Mean Stack by Bitnami

After setting up the Mean Stack Manager, I encountered an issue. When running a command in the node terminal like console.log("Hello World"), it runs smoothly. However, if I place a Javascript sample file in any folder within the mean stack install directo ...

What is the best way to create a sliding motion to the right for a menu item, accompanied by a line when it

Is there a way to align the line to the right of the text when it's not selected, and have a new line appear on the left side that pushes the text and removes the line on the right side? Here is the design I'm trying to emulate. If you click t ...

Attempting to connect JQuery to a different page through a data attribute connection

I am currently working on a slider that connects slides from an external page through data attributes. Here is the setup: Main Page <div id="sitewrapper"> <section class="sliderwrapper"> <div id="slider" data-source="slides-pa ...

What is the procedure for incorporating JavaScript into my tic-tac-toe game's HTML and CSS?

I have created a simple Tic Tac Toe game using HTML and CSS. Although there is no embedded Javascript, I do reference a Javascript file for functionality. Can anyone provide me with the most straightforward Javascript code that will display an 'X&apos ...

When retrieving data from MongoDB using Map.get() in Node.js, it consistently yields undefined

Within this map, I have stored id:quantity pairs. My goal is to add the quantity from the map to each item in an array of products (which contains objects). Despite having the same value and data type for the id, when using the map.get(id) method, it retur ...