scrollTop always returns 0

Viewed 23529

I've tried using scrollTop as well as scrollTop() and it always returns 0. How can this be, I've tried it like this:

Template.myHome.events({
    "click .examine": function(event){
        event.preventDefault();

        varPos = event.clientY;
        var yPos = document.body.scrollTop;
        console.log("Y coords: " + yPos);
    }
});

as well as using the JQuery version on several div's within and including the body tag, again all giving me 0 or null.

All I simply want is to get the number of pixels traversed on the y-axis as the user scroll downs the page.

5 Answers

function topScrollFunction() {
                if (document.documentMode || /Edge/.test(navigator.userAgent)){
                  $('body').css('height', 'auto'); 
                 window.scrollTo(0,0);
              }else {
                window.scrollTo(0,0);
              }
            }

  1. Added a condition for edge browser.Since you dont wanna impact the other browsers with the height
  2. You need to add $('body').css('height', 'auto'); and then check for document.body.scrollTop which will give you the correct scroll position value. Hope that helps.Hope this works for you. Happy Coding.

You need to add following CSS to body tag

body {
height: auto !important;
}

then check for document.body.scrollTop which will give you the correct scroll value

Related