简体   繁体   English

如果文档可以垂直滚动 200px 或更多运行功能

[英]If document can be scrolled vertically by 200px or more run function

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.使用 jQuery 或普通 JavaScript 我想在document ready运行一个函数,如果页面有垂直滚动条并且页面可以滚动的量是200px或更大。

Basically, I want to modify the code below so that it runs on document ready , not when the user actually scrolls.基本上,我想修改下面的代码,使其在document ready时运行,而不是在用户实际滚动时运行。

$(document).ready(function(){
  $(window).scroll(function(){
    if ($(this).scrollTop() > 200) {
      // do stuff
    }
  });
});

This is how i would do it.这就是我要做的。

$(document).ready(function() {
  if(($(document).height() - $(window).height()) > 200)
  {
     //Do something.
  }
});

You need to trigger the scroll for window您需要trigger window scroll

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

$(window).scroll(function(){
     if ($(this).scrollTop() > 200) {
       // do stuff
     }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM