0

I want to know a code for continuous website that if i scroll to the bottom and will get back to top, and otherwise.

i already in a half way that i used this code for the bottom page :

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

how can i make conversely? from top to the bottom page?

EDIT:

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

i tried that code and it works, but i think there is a little bug there, i need to scroll a bit more to get to bottom from top or otherwise, why is that happen?

Thanks

Mumir
  • 1
  • 3

2 Answers2

0

scrollTop can't go below 0, so you probably want to leave a buffer at the top of the page which sends you to the bottom when you scroll above it.

Something like this:

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());
    }
  });
});
Jon Molnar
  • 193
  • 1
  • 9
0

i think this will help

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

or if you want to use jQuery get it from this thread

Community
  • 1
  • 1
AKHIL K
  • 84
  • 1
  • 2
  • 11