0

Using jQuery or plain JavaScript I want to run a function on document ready, if the page has a vertical scrollbar and the amount by which the page can be scrolled is 200px or larger.

Basically, I want to modify the code below so that it runs on document ready, not when the user actually scrolls.

$(document).ready(function(){
  $(window).scroll(function(){
    if ($(this).scrollTop() > 200) {
      // do stuff
    }
  });
});
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Malasorte
  • 1,153
  • 7
  • 21
  • 45
  • This could help: http://stackoverflow.com/questions/2146874/detect-if-a-page-has-a-vertical-scrollbar – sinisake May 26 '16 at 06:39

2 Answers2

2

This is how i would do it.

$(document).ready(function() {
  if(($(document).height() - $(window).height()) > 200)
  {
     //Do something.
  }
});
Jesper Højer
  • 2,942
  • 1
  • 16
  • 26
0

You need to trigger the scroll for window

$(document).ready(function(){
   $(window).trigger('scroll');
   //or $(window).scroll();
});

$(window).scroll(function(){
     if ($(this).scrollTop() > 200) {
       // do stuff
     }
});
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
  • This still triggers on scroll? Asker wanted to check if a user could scroll, not actually if he scrolls but on pageready – Randy May 26 '16 at 06:40
  • @randy OP is saying _I want to modify the code below so it runs on document ready, not when the user actually scrolls_ i.e. he does not want to wait for user to scroll is what I presume.. – Guruprasad J Rao May 26 '16 at 06:42