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

Adding animation effects using CSS and/or jQuery

Are there any Jquery plugins or CSS codes available to achieve similar effects as seen on the following pages and , without utilizing webGL?... ...

Positioning the filters in jQuery Datatables

I'm currently working with jQuery datatables and I'm attempting to align the filter/search box on the same row as the header of the container holding the datatable. Attached is a screenshot for reference: https://i.stack.imgur.com/nzbIl.png He ...

What is the most effective way to determine which radio button is currently selected in a group with the same name using JQuery?

<form class="responses"> <input type="radio" name="option" value="0">False</input> <input type="radio" name="option" value="1">True</input> </form> Just tested: $('[name="option"]').is(':checked ...

Using the Table-multiple-sort feature in boostrap-table is not functioning properly when there are multiple tables present on a single page

I have implemented bootstrap-table along with the extension table-multiple-sort. The issue I am facing is when I include two tables on a single page (with the second table within a modal window), the multisort feature does not seem to work on the second ta ...

Getting a variable from an ajax call and using it in a Pug template

Currently, I am experimenting with Express and Pug to learn some new concepts. Upon making a request to a website, I received some dynamic information stored in an array. However, I am facing challenges when trying to iterate over this array using Pug. The ...

Attempting to grasp the sequence in which setTimeout is ordered alongside Promise awaits

I've been puzzling over the sequence of events in this code. Initially, I thought that after a click event triggered and Promise 2 was awaited, the for loop would resume execution once Promise 1 had been resolved - however, it turns out the outcome is ...

Using VueJS for reactive binding效果

I am attempting to assign a class using the following syntax: :class="{active: favs.medium_title.fontWeight === 'bold'}" However, the fontWeight attribute is not yet defined when the component loads. This is an excerpt from my object: favs: { ...

Having trouble with the full-screen feature not functioning properly?

I am currently in the process of creating a custom video player and I need to include a full-screen button. However, when I click on it, the video does not expand to fill up the entire screen. I am using javascript, css3, and html5 for this project. Any as ...

Guide on showing the D3 Color legend in a horizontal orientation instead of a vertical one

I'm currently in the process of creating a color legend using d3, and I've managed to display it vertically. However, I would like it to be shown horizontally instead. Below is a snippet of the code I've been working on along with a link to ...

The true screen resolution of Safari on iPhone devices

I'm feeling a bit lost right now. Currently, I am using Jquery Mobile and here is my code: <meta content='width=device-width, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no' name='viewport'> When I ran the following ...

What is the best way to combine jQuery objects in order to apply CSS styles collectively?

If I have a custom function that takes in multiple jQuery objects: customFunction($('.content'), $('h1'), $('input[type=button]')); The function is defined as follows: function customFunc ...

Simply touch this element on Android to activate

Currently utilizing the following code: <a href="http://www...." onmouseover="this.click()">Link</a> It is evident that this code does not work with the Android Browser. What javascript code will function properly with the Android Browser as ...

"Utilize JavaScript to structure JSON, eliminate redundant entries, and calculate the total number

I'm feeling a bit disoriented, as I have this dataset in json format with timestamps and ids arranged like so: [{ "date":"2016-11-18 19:20:42","id_pa":"7" },{ "date":"2016-11-18 19:04:55","id_pa":"5" },{ "date":"2016-11-19 20:53:42","id_pa":"7" ...

Steps to extract date selection from a datepicker using jQuery

I recently implemented this code snippet to utilize datepicker for displaying dates: $(document).ready(function(){ $("#txtFrom").datepicker({ numberOfMonths: 1, onSelect: function (selected) { var dt = new Date(selected); ...

Display the closest locations on the Google Maps interface

I am currently working on a project that involves integrating Google Maps. The project includes storing hospital addresses (in longitude and latitude) in a database. However, I need assistance in displaying the nearest hospital from my current location. I ...

Adjust the position of the element by lifting it slightly

I am looking to adjust the positioning of the number "01 / 04" to match the layout shown in this image: Here is what I have accomplished so far: This is how the code structure looks like in vue js at the moment: <img id="theZoomImage" sizes="100vw" : ...

Automatic Update Division in Jade Template (Node.js/Express)

In my development project using Node.js, Express, Jade, and Redis, I have successfully created an app that pulls telnet stream data from reversebeacon.net. This data is then cross-referenced with Redis databases containing amateur radio club member informa ...

Is it feasible to assess JavaScript expressions during compilation using Webpack?

I'm currently in the process of setting up webpack for a new project. This project is quite extensive and available in multiple languages. I am aiming to have each entry point in my project be represented by separate files in each language. The catch ...

Attach the div element firmly to the bottom of the webpage

I'm trying to place a second div under one with position absolute, but the two divs have different widths. The top div is centered and positioned absolutely, while the bottom div should span 100% of the width. Any suggestions on how to achieve this? ...

Determine the dimensions of a div element in IE after adjusting its height to auto

I am currently working on a JavaScript function that modifies the size of certain content. In order to accomplish this, I need to obtain the height of a specific div element within my content structure. Below is an example of the HTML code I am dealing wit ...