-1

I am looking at the code written by someone else. In this a class is added of removed when we reach a particular section.I cannot understand the use of $('body').height() here

if($('#impact_calculator'). length){
        $(window).scroll(function () {
        var scroll = $(window).scrollTop();
        // var box = $('#impact_calculator').offset().top -190;
        var box = $('body').height() - $('#impact_calculator').height() - 450;
        if (scroll >= box) {
            setTimeout(() => {
            $("body").addClass("sticky");
            }, 500);
        } else {
            setTimeout(() => {
                $("body").removeClass("sticky");
            }, 500);
        }

    });
  • The window is the visible viewport of your device, think of your smartphone. The body can be much larger. That's why you sometimes see scrollbars. – Marco Sep 21 '22 at 06:54
  • Please search before posting a new question. Searching for your exact title turns up many answers here already: https://stackoverflow.com/questions/14504195/what-is-the-difference-between-document-height-and-window-height, https://stackoverflow.com/questions/14035819/window-height-vs-document-height, https://stackoverflow.com/questions/11023183/jquery-body-height-returns-undefined, ... – Don't Panic Sep 21 '22 at 11:56
  • I know the difference between window and document height , i was confused with the keyword body. Is $("body").height() same as $("document").height() ? this was my main confusion – Vishant Sehrawat Sep 24 '22 at 16:01

1 Answers1

0

Kindly refer to this link for your reference: https://api.jquery.com/height/

// Returns height of browser viewport
$(window).height();

Usually, $(window).height() delivers the browser window's height less one pixel. The height of the current browser window is always this. This value do need to alter as your browser is resized.

// Returns height of HTML document
$(document).height();

$(document).height() returns an unit-less pixel value of the height of the document being rendered.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31