Javascript 页面加载时自动滚动到底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29291020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically scroll to bottom as the page loads
提问by LPChip
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.
我有一个 php 脚本,它在脚本运行时显示其操作的日志。日志变得很长,直到它会在页面底部回显其信息,向页面添加滚动条。
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?
如果我想查看日志的其余部分,我必须手动向下滚动。我可以让页面说... process.php#bottom 但我不能只是<a name="bottom" />在每个日志项之后插入一个并期望浏览器一直跳到底部,可以吗?
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?
是否有一个 javascript 函数或其他简单的方法,如果不是这种情况,它会自动将页面滚动到底部?
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.
我不介意它是否会覆盖用户的滚动能力,因为无论如何脚本都会在 3 秒后重定向回主脚本。
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.
基本上,随着脚本每秒加载和休眠,当它到达页面底部时,它应该每秒自动向下滚动。
回答by Azazi
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>
回答by mohamedrias
Every 5 seconds, it will scroll the page to the bottom of the page.
每 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>
回答by userx01
I think what you're looking for is this: http://www.w3schools.com/jsref/met_win_scrollto.asp
我想你要找的是这个:http: //www.w3schools.com/jsref/met_win_scrollto.asp
And then, used with conjunction with this: http://www.w3schools.com/jsref/prop_element_scrollheight.asp
然后,与此结合使用:http: //www.w3schools.com/jsref/prop_element_scrollheight.asp
You can make something like:
你可以做这样的事情:
function scrollToBottom{
window.scrollTo(0, document.body.scrollHeight);
}
回答by lal rishav
function fun()
{
$("div").scrollTop(50);
}
now use
现在使用
<body onload="fun">

