页面加载时自动滚动到底部


Automatically scroll to bottom as the page loads

我有一个php脚本,它在脚本进行时显示其操作的日志。日志越来越长,以至于它会在页面底部回显信息,并在页面上添加滚动条。

如果我想查看日志的其余部分,我必须手动向下滚动。我可以让页面显示。。。process.php#bottom,但我不能只是在每个日志项后插入一个<a name="bottom" />,并期望浏览器不断跳到底部,对吗?

有没有一个javascript函数或其他简单的方法,一旦不是这样,就会自动将页面滚动到底?

我不介意它是否会覆盖用户的滚动能力,因为无论如何,脚本都会在3秒后重定向回主脚本。

如果你只有指针,我不一定需要一个完整的脚本,但那些提供完整工作脚本的人显然会比那些只提供指针的人得到接受。

如果你不知道我所说的日志是什么意思,你可以使用以下脚本来模拟我的脚本:

<?php
for( $iLineNumber=0 ; $iLineNumber <100 ; $iLineNumber++ )
{
    echo $iLineNumber , ' - This is a test. <br />';
    sleep(1);
}
?>

基本上,当脚本每秒加载并休眠时,当它到达页面底部时,它应该每秒自动向下滚动。

这是有效的:

<script>
    setTimeout(printSomething, 1000);
    function printSomething(){
        for(var i=0; i<10; i++){
            document.write("Hello World'n");
            document.write("<br>");
        }
        window.scrollTo(0,document.body.scrollHeight);
        setTimeout(printSomething, 1000);
    }
</script>

每隔5秒,它就会将页面滚动到页面底部。

function autoScrolling() {
   window.scrollTo(0,document.body.scrollHeight);
}
setInterval(autoScrolling, 5000); 
// adjust the time limit based on the frequency log gets updated
  <html>
   <body>
      <script type="text/javascript">
         setInterval(function() {
            document.body.innerHTML += "<p>New log</p><br>"
         }, 5000);
      </script>
   </body>
  </html>

我想您想要的是:http://www.w3schools.com/jsref/met_win_scrollto.asp

然后,与此结合使用:http://www.w3schools.com/jsref/prop_element_scrollheight.asp

你可以制作这样的东西:

    function scrollToBottom{
         window.scrollTo(0, document.body.scrollHeight);
    }
function fun()
{
 $("div").scrollTop(50);
}

现在使用

<body onload="fun">

2021年,你也可以在脑海中这样做:

    setTimeout(() => {
        window.scrollTo(0, document.body.scrollHeight);
    }, 100);

<!DOCTYPE html>
<html>
<head>
<style>
body {
    font-family: Verdana, sans-serif;
    font-size: 14px;    
}
</style>
<script>
    setTimeout(() => {
        window.scrollTo(0, document.body.scrollHeight);
    }, 100);
</script>
</head>
<body>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
A lot of test goes here<br>
</body>
</html>