永不结束的滚动页面(滚动页面的顶部和底部或其他方式)



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

我已经在底部使用了这段代码的一半:

$(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());
                  }
                });
              });

我尝试了该代码并且它可以工作,但我认为那里有一个小错误,我需要再滚动一点才能从顶部或其他地方到达底部,为什么会这样?

谢谢

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());
    }
  });
});

我认为这会有所帮助

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

或者如果你想使用jQuery从这个线程中获取它

最新更新