Home javascript JavaScript Scroll down the page

JavaScript Scroll down the page

Author

Date

Category

How to scroll down a web page using pure java script. The page is built dynamically as you scroll down. I have sketched the code below, but there is a problem – document.body.scrollHeight does not change. Those. scrolls once. Therefore, dancing with a delay of 100 milliseconds, I thought that it took time to reload. But even suspending by alert does not help.

function scrollToEndPage (height, attempt) {
alert ("hight:" + height + "scrollHeight:" + document.body.scrollHeight + "att:" + attempt);
if (height! = document.body.scrollHeight)
{
  height = document.body.scrollHeight;
  window.scrollTo (0, height);
  attempt ++;
  setTimeout (scrollToEndPage (height, attempt), 100);
}
}

Answer 1, authority 100%

You can do this

var height = 15;
var attempt = 4;
var intS = 0;
function scrollToEndPage () {
console.log ("hight:" + height + "scrollHeight:" + document.body.scrollHeight + "att:" + attempt);
if (height & lt; document.body.scrollHeight)
{
  // height = document.body.scrollHeight;
  window.scrollTo (0, height);
  attempt ++;
  height = parseInt (height) + attempt;
}
esle
{
  clearInterval (intS);
}
}
intS = setInterval (scrollToEndPage, 100);

Answer 2

It turned out to be implemented through setTimeout as well.
You can’t do this:

setTimeout (scrollToEndPage (height, attempt)}, 100);

Needed:

setTimeout (function () {scrollToEndPage (height, attempt)}, 100);

Answer 3

Use the following code to load and every page refresh:

el.scrollTop = Math.ceil (el.scrollHeight - el.clientHeight);

Where el is a page or element where you want to scroll down everything, and rounding is needed for positioning accuracy on high-resolution screens. scrollHeight is requested every time the page is refreshed.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions