【问题标题】:never ending scrolling page (scrolling top and back to bottom of the page or otherwise)永无止境的滚动页面(滚动到页面的顶部和底部或以其他方式)
【发布时间】:2015-04-28 17:45:33
【问题描述】:

我想知道一个连续网站的代码,如果我滚动到底部并返回顶部,否则。

我已经在一半的地方使用此代码作为底部页面:

$(document).ready(function() {
    $(document).scroll(function(){
      if (document.documentElement.clientHeight + $(window).scrollTop() >= $(document).height()) {
        $(document).scrollTop(0);
      }
    });
  });

我怎么能反过来呢? 从顶部到底部页面?

编辑:

$(document).ready(function() {
                $(document).scroll(function(){
                  if (document.documentElement.clientHeight + $(window).scrollTop() >= $(document).height()) {
                    $(document).scrollTop(0);
                  }
                  else if ($(window).scrollTop() <= 0) {
                    $(document).scrollTop($(document).height());
                  }
                });
              });

我尝试了该代码并且它可以工作,但我认为那里有一个小错误,我需要再滚动一点才能从顶部到达底部,否则为什么会发生这种情况?

谢谢

【问题讨论】:

    标签: javascript jquery html infinite-scroll


    【解决方案1】:

    scrollTop 不能低于 0,因此您可能希望在页面顶部留下一个缓冲区,当您在其上方滚动时,它会将您发送到底部。

    类似这样的:

    var BUFFER = 10;
    
    $(document).ready(function() {
      $(document).scrollTop(BUFFER);
      $(document).scroll(function(){
        var scrollPos = document.documentElement.clientHeight + $(window).scrollTop();
        if (scrollPos >= $(document).height()) {
          $(document).scrollTop(BUFFER);
        } else if (scrollPos < BUFFER) {
          $(document).scrollTop($(document).height());
        }
      });
    });
    

    【讨论】:

    • 我试过了,但没用,我编辑了我的代码,检查一下兄弟
    【解决方案2】:

    我认为这会有所帮助

    window.scrollTo(0,document.body.scrollHeight);
    

    或者如果你想使用 jQuery 从this thread获取它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      相关资源
      最近更新 更多