簡體   English   中英

頁面加載時自動滾動到底部

[英]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);
}
?>

基本上,隨着腳本每秒加載和休眠,當它到達頁面底部時,它應該每秒自動向下滾動。

每隔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> 

這有效:

<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>

我想你要找的是: 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM