在页面加载时自动滚动到底部 [英] Automatically scroll to bottom as the page loads

查看:584
本文介绍了在页面加载时自动滚动到底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php脚本,在脚本进行时显示其操作日志。日志变得越来越长,直到它将在页面底部回显信息,向页面添加一个滚动条。



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

是否有一个JavaScript函数,或其他简单的方法,将自动滚动页面到底部一旦不是这种情况?

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



我不一定需要一个完整的脚本,如果你只有指针,但那些提供完整工作脚本的脚本显然会获得它们的回答接受那些只是指针。



如果你不知道我的意思是什么日志,你可以使用下面的脚本来模拟我的($ iLineNumber = 0; $ iLineNumber <100; $ iLineNumber ++)

 <?php 

{
回声$ iLineNumber,' - 这是一个测试。 < br />';
sleep(1);
}
?>

基本上,当脚本每秒钟加载并睡眠时,当它触及页面底部时,应该每秒钟自动向下滚动。

解决方案

这样做:

 <脚本> 

setTimeout(printSomething,1000);

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


I have a php script that shows a log of its actions as the script proceeds. The log is getting quite long, up to the point where it will echo its information past the bottom of the page, adding a scrollbar to the page.

If I want to see the rest of the log, I have to manually scroll down. I can make the page go to say... process.php#bottom but I can't just insert a <a name="bottom" /> after each log item and expect the browser to keep jumping to the bottom, can I?

Is there a a javascript function, or other easy method, that will automatically scroll the page to the bottom as soon as this isn't the case?

I don't mind if it overrides the user's ability to scroll, as the script will redirect back to the main script after 3 seconds at the end anyway.

I do not necessarily need a full script, if you just have pointers, but those that provide a full working script will obviously get their answer accepted over those that just give pointers.

If you don't have an idea of what I mean by the log, you can use the following script to simulate what my script does:

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

Basically, as the script loads and sleeps every second, when it hit the bottom of the page, it should automatically scroll down every second.

解决方案

This works:

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

这篇关于在页面加载时自动滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆