Exclude footer height when implementing an infinite scroll feature using Jquery Ajax

I'm currently tackling the challenge of implementing ajax infinite scroll. I have the following code in place to trigger an ajax request once scrolling reaches the end:

$(window).scroll(function(){
if($(window).scrollTop() >= ($(document).height() - $(window).height())){
    loadlist();
}
});

However, the issue I'm facing is that it fires when scrolled all the way to the end (including the footer). What I actually want is for it to trigger when the footer is just starting to appear while scrolling (considering the footer height is 300px).

I did some research and attempted the following solution:

$(window).scroll(function(){
if($(window).scrollTop() >= ($(document).height() - $(window).height()) - 300){ // accounting for 300px footer height
    loadlist();
}
});

Despite this workaround, it doesn't seem quite right as the function gets called too frequently during scrolling. Are there any better alternatives or solutions available?

Answer №1

To enhance the user experience, consider triggering the desired behavior once the footer element becomes visible during scrolling.

const $footer = $("#my-footer");

$(window).scroll( function() {
    if (isElementInViewport($footer) ) executeAction();
});

For a helpful code snippet to check if an element is in view while scrolling, refer to Check if element is visible after scrolling.

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

The standard color code applied to an HTML button

I need to create an anchor element <a> that resembles an HTML button. However, I am struggling to find the default background color for the input type button. ...

After a certain period of time, the NodeJs exec() function ceases to create additional

I am in the process of developing a BLE scan module on nodeJs using Bluez. Below is the code snippet I have implemented: exec('sudo hcitool lescan --duplicates &', function (error, stdout, stderr) { }); exec('sudo hcitool lescan --dupl ...

Angular: Design dependent on attributes

Can I customize the styling of a div in accordance with a boolean property called "isActive" on my controller using Angular? <div class="col-md-3" (click)="isActive = !isActive"> <div class="center"> <i class="fa fa-calendar"& ...

Are you searching for a stylish tabbed navigation menu design?

I am searching for a tabbed navigation menu specifically designed to showcase images as the tab items. Here is an example of what I have in mind: <div class="menu"> <a href="#"> <img src="images/Chrysanth ...

unable to view the contents of the template using the directive

I am facing an issue with the Directive, as it is not adding an element to the Template. This directive has been included in the .html file. There are no errors being displayed in the console. directive.js: app.directive('myDirective', function ...

What is the best way to expand the size of the pagination buttons in a primeng table?

My primeng table is quite large: <p-table #dt [value]="artefacts" [columns]="cols" [paginator]="true" [rows]="numberRows" [tableStyle]="{'table-layout':'auto'}"> With so many entries, the table spans over 100 pages. However, the ...

Interaction of PHP and JavaScript when a new row is inserted using JavaScript

I have encountered an issue with the following PHP code snippet: PHP Code: <?php $output['house_sitting_date_yes_no']=$_POST['house_sitting_date_yes_no']; if(file_exists('../feeds/ptp-ess_landing_house.json')){ ...

Communicate through Node.js, with messages that multiply based on the total number of users currently in the chat room

Currently, I am working on a project that involves creating a chat application using Node.js. However, I have run into an issue where the message gets repeated for each user in the chat. For example, if there are 4 users in the chat, the message will be di ...

Auto-suggest feature using Jquery for dynamically pulling suggestions from a database table

How can I implement an auto-suggest text field that can fetch usernames from my database? I intend to utilize the autocomplete plugin from Jquery UI for this purpose. My aim is to create a fast and highly secure solution. ...

illuminate the area chart lines when they intersect in highcharts

There's a specific area in my highcharts creation that I'm struggling with. Whenever one line crosses over another, the color becomes lighter. How can I keep the line color consistent, even when highlighting it on hover? Also, I'm looking t ...

A guide to storing a JavaScript variable in a MySQL database using Express.js

Looking for some guidance on node js and expressjs framework. While developing a web application, I've encountered an issue with saving data into the database. Everything seems to be set up correctly, but the data stored in the variable (MyID) is not ...

Creating a dynamic layout with Bootstrap 4 featuring a full-page design, two columns, and a

For a simple "login" page, I came across this template: HTML: <div class="d-flex align-items-center flex-column justify-content-center h-100 bg-dark text-white" id="header"> <h1 class="display-4">Hello.</h1 ...

execute an operation without navigating away from the page when a comment is created

Hey there! I am currently using Facebook comments on my website and I am looking for a way to send the page URL to my database or a text file without redirecting the page. I have tried various methods but haven't had much luck. Can anyone help me out ...

Tips for testing a service in Angular using unit testing techniques

Within my service, I have a function that looks like this: exportPayGapDetails(filterObject: PayGapDetailFilter): void { const url = `${this.payGapDetailExportUrls[filterObject.type]}`; this.http .post<PollInitResponse>( `/adpi/rest/v2/s ...

Guide on submitting a query to an ASP page through PowerShell

One of the pages on an internal aspx website is http://stackoverflow/answers/Requests.aspx. This page is where data is submitted through forms to retrieve results. I am seeking advice on how to submit the same query/data using PowerShell. Although I am ...

Delay the jQuery event handler for a few milliseconds before triggering

I'm currently working on a navigation menu, but I've encountered some bugs and I'm struggling to fine-tune it completely. Here are the issues I'm facing, and any help or solutions would be greatly appreciated. The menu items (About ...

Error in Taiwlind CSS: Styles are not loading in production environment but are working fine in development environment. This issue is encountered while using Next.js in

After reading various blog posts online, I put together this code snippet. The confusion arises from blogs using autoprefixer while others use postcss-preset-env. In my setup, I have added tailwindcss as a development dependency and properly imported the ...

I encountered a response error code 500 from the development server while using my emulator

As I embark on setting up the react-native environment for development, I encounter an error when executing the command react-native run-android. root@pc:~/l3/s2/DevMobMultipltm/Wakapp# ` A series of tasks are carried out including scanning folders for sy ...

Reasons for dividing by 1 in JavaScript

While completing a basic programming assignment from my teacher, I encountered an interesting issue in Javascript. I found that when dividing a number by 1, it returns an unexpected value. Can anyone provide an explanation for this? I have created a jsfidd ...