The total height of the document's body in jQuery is not equal to the sum of the viewport height and the window's scroll top position at the bottom of the document

Why does the document height appear smaller than the window scroll top value plus the viewport height when I reach the end of the document? Shouldn't they be equal? I've been struggling with this issue for hours and can't seem to figure it out.

$(function(){               
            vpw = parseFloat($(window).width());
            vph = parseFloat($(window).height());
            appearh = parseFloat(vph*0.4);
            dh  = $(document).height();
            footerh = $('#footer-area').height();
            footTop = dh - footerh;
            resizeDiv(vpw, vph, appearh);


            $(window).scroll(function(){
                scrollPos = $(window).scrollTop();
                jj = vph + scrollPos;
                console.log(scrollPos + '+' + vph + '=' + jj + ' is (at the bottom) ' + dh);
                if(scrollPos > appearh){
                    addWin = parseFloat(dh - vph);
                    $('#trends').removeClass('hidetrends',2000).addClass('showtrends',2000);
                    /*console.log( dh + '>' + scrollPos + ';' + addWin );
                    if(scrollPos >= 1672){
                        $('#trends').css('position', 'relative');
                    }else if(scrollPos <= 1672){
                        $('#trends').css('position', 'fixed');
                    }*/
                }else{
                    $('#trends').removeClass('showtrends',2000).addClass('hidetrends',2000);
                }

            });
        });
        window.onresize = function(event) {
            resizeDiv(vpw, vph, appearh);
        }
        function resizeDiv(vpw, vph, appearh) {
            $("#full-width").css({"height": vph + "px"});
        }

Answer №1

Here's a suggestion for you to try out:

$(window).scroll(function(){
     let windowWidth = parseFloat($(window).width());
         let windowHeight = parseFloat($(window).height());
         let appearHeight = parseFloat(windowHeight*0.4);
         let documentHeight  = $(document).height();
         let footerHeight = $('#footer-area').height();
         let footerTop = documentHeight - footerHeight;
         resizeDiv(windowWidth, windowHeight, appearHeight);
         let scrollPosition = $(window).scrollTop();
         let totalHeight = windowHeight + scrollPosition;
         console.log(scrollPosition + '+' + windowHeight + '=' + totalHeight + ' is (at the bottom) ' + documentHeight);
....

The idea here is to update your variables as you scroll through the page.

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

Automatically append version number to requests to avoid browser caching with Gulp

When deploying my project, I utilize gulp to build the source files directly on the server. In order to avoid caching issues, a common practice is to add a unique number to the request URL as explained in this article: Preventing browser caching on web app ...

When using jQuery, you can utilize the mouse over event to display elements with the same class within an each loop

I am struggling with a hover effect on my webpage. I have three visible elements and three hidden elements, and I want each visible element to display only one hidden element when hovered over. However, the issue is that currently, hovering over a visible ...

What is the best way to conceal a sticky footer using another element?

I have a footer that remains fixed at the bottom of both long and short pages. Below is the CSS code for this footer: .footer{ position: absolute; bottom: 0; width: 100%; height: 100px; background-color: white; } My goal is to achieve a 'r ...

Utilizing React Views in an Express Environment

I am struggling to find a simple example that demonstrates how to connect an Express.js route to render a React view. This is what I have tried so far. +-- app.js +-- config | +-- server.js +-- routes | +-- index.js +-- views | +-- index.html app. ...

Best practices for managing non-PrivateRoutes in React

In my app.js, I have set up a PrivateRoute that requires user login and only grants access if the cookie is set. However, I want to prevent users from accessing /login after they have logged in successfully. To achieve this, I implemented a LoginRoute wi ...

Who is the father of Bootstrap Popover?

I'm currently facing an issue with getting a popover to be placed within a specific element. As of now, the popover is being inserted directly into the <body>, but I require it to be relative to other elements. I have been unable to locate a met ...

The left side of the form input material is obscured unless the necessary criteria are fulfilled

I am currently in the process of developing the front-end for a web application using the material library. The issue I am facing is that when a field does not meet its requirements, such as needing to enter a 'bedrijfsnaam', the border turns red ...

unable to make a request to the express server with axios

I am in the process of developing a chat application similar to whatsapp. One of the key features I'm working on is that when a user clicks on another person's name, their chats will be displayed. However, currently, I'm facing an issue wher ...

Assist me with Twitter

Seeking assistance with a coding issue related to displaying Twitter posts on a website. I have found a code snippet that accomplishes this: //Scrolling of tweets in the footer $(function () { var tweetVP = $("#footerTweetsViewport"); arrTweetNav ...

Header element not keeping the navbar at the bottom

My goal is to attach this navigation bar to the bottom of a header, but it's sticking to the top instead. I want the navigation to remain at the bottom of the header even when the user scrolls down. Currently, both the header and navigation are set t ...

A solution for accessing computed properties within a v-for loop

Currently, I am facing a challenge with the code provided below. It seems that computed properties do not support parameters. Do you happen to have any suggestions on how to overcome this issue? I am considering using watchers on functions but I am also ...

What is the process for setting a specific version of Node for a project on my local machine?

I am currently facing an issue with setting up Node across multiple developers' machines for a project. The challenge lies in the fact that not all team members are experienced in Node or JavaScript, and we need to ensure that everyone has the correct ...

Resolving the issue of nested modals within Bootstrap

I am experiencing some issues with my customer visits list page. When I try to add a visit, it opens a bootstrap popup ("#Pop1") to record the visit details. Within this modal, there is an option to add a new customer on the spot, which then triggers anoth ...

Passport.js simply redirects to the failure page without invoking the LocalStrategy

I'm facing a persistent issue with Passport.js in my Express.js small application. No matter what I input in the LocalStrategy, I always end up being redirected to the failureRedirect without seemingly passing through the LocalStrategy at all. What am ...

Refreshing HTML tables with AJAX in Django

After going through all the posts here, I found that some are outdated and others don't quite match my issue. Apologies for bringing it up again. I have a basic HTML table with data in it. My goal is to update this data without refreshing the entire ...

Enable/disable dropdown menu based on choice of radio button label

I've been attempting to disable and enable a select field based on radio input labels generated by a pdo-query, but haven't been able to get it working correctly. Disabling the select field works using prop('disabled', true), but enabl ...

When implementing flex-grow 1 on a flex box, the remaining space is not being filled as expected

I am struggling to make the green div fill the remaining space of a flex box container. My goal is to have the top row (blue) adjust its height based on content, and then have the row below (green) take up the rest of the space. However, it seems like the ...

Challenges when Implementing JQuery Tab Components

I am currently working on adding new tabs to my existing tab system created with jQuery. My approach involves assigning a class "extend" to the anchors that will create the new tabs and removing their default functionality. I then load the content into t ...

Do I need to manually destroy the directive scope, or will Angular take care of it

I have a question about directives in Angular. Let's say we have a directive called "myDirective". Here is the corresponding HTML: <div my-directive> </div> When we remove this <div> element from the DOM, will Angular automatically ...

When using JSON.stringify on a map object, it returns an empty result

var map1= new Map(); map1.set("one",1); var map2 = new Map(); map2.set("two",2); concatMap = {}; concatMap['one']= map1; concatMap['two']= map2; JSON.stringify(concatMap); //outputs : "{"one":{},"two":{}}" I als ...