1

I have searched about this topic and all solutions that I got is based on time intervals. I have a php page which is based on a query. The results of this query can be small or huge depending on its parameters. The generated page must be scrolled down smoothly until the results end and scrolled top again and repeat. If the solution is based on time interval, the page can be scrolled and the results can not be shown completely.

One of the solutions, based on time intervals, that I got is this: Autoscroll to bottom of page then top and repeat. I am ttrying using javascript or jquery, using the code below:

<script>
var pix = 0;
var sh = document.body.scrollHeight;
var ch = document.body.clientHeight;

setInterval(function(){  
    pix = pix+1;
    //console.log("sh="+sh+" ch="+ch+ " pix="+pix);
    if (pix<=sh)
      window.scrollBy(0, 10); 
    else {
      pix = 0;
      $(document.body).scrollTop(0);
    }
  }, 1000);
</script>

The first part is ok: the scroll goes smoothly. But when the page gets the end, it did not start again ($(document.body).scrollTop(0);)

Best regards, thanks.

Victoria Ruiz
  • 4,913
  • 3
  • 23
  • 40
Fabiano
  • 65
  • 5
  • How is this not working for you? Please explain what the current behavior is, and how it differs from the desired one. – Victoria Ruiz Jul 01 '18 at 18:46
  • The first part is ok: the scroll does smoothly but when the page gets the end, it did not start again($(document.body).scrollTop(0);) – Fabiano Jul 01 '18 at 22:06
  • Did you include jQuery in your header? You suddenly switch to it in only that line. On the other hand, it is a good Stack Overflow practice to edit your question to include any further information, instead of putting it in comments ;-) – Victoria Ruiz Jul 02 '18 at 14:26
  • Yes, jquery is on the header. Sorry, but what information did I put in comments instead of message body? – Fabiano Jul 02 '18 at 14:43

1 Answers1

0

You need to change the scrollTop line to this:

$(document).scrollTop(0);

Since that line uses jQuery, make sure it is called in your header or footer.

Also, you're only using jQuery for that particular line, so if you wanted to choose vanilla JS, you could use this instead:

window.scrollTo(0,0)
Victoria Ruiz
  • 4,913
  • 3
  • 23
  • 40
  • Thanks Victoria. I have already tried other approaches and that only line with jquery was the last one from one of my approaches. Your answer is great and it is correct, but how do I do to make the scroll be more smoothly? – Fabiano Jul 02 '18 at 18:03