Javascript 如何使用 window.scroll 在页面加载时自动滚动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6302930/
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
How to use window.scroll to automatically scroll on pageload?
提问by TaylorMac
function Scrolldown() {
window.scroll(0,300);
}
How can I use this function (or a similar one) to automatically scroll down when the page loads? (without clicking a link)
如何使用此功能(或类似功能)在页面加载时自动向下滚动?(无需点击链接)
Regards,
问候,
taylor
泰勒
采纳答案by Michael Robinson
Give this a try:
试试这个:
function Scrolldown() {
window.scroll(0,300);
}
window.onload = Scrolldown;
回答by user2445247
you could include all of your javascript in an event. This will scroll to an element with an id.
你可以在一个事件中包含你所有的javascript。这将滚动到带有 id 的元素。
<body onload="window.location='#myID';">
回答by Casey Chu
You could use window.onload
:
你可以使用window.onload
:
window.onload = Scrolldown;
I also want to point out that you could use an HTML anchor to indicate where to scroll to, so you don't have to hard-code the pixel value:
我还想指出,您可以使用 HTML 锚点来指示滚动到的位置,因此您不必对像素值进行硬编码:
<div id="iWantToScrollHere" name="iWantToScrollHere">
...
</div>
...which would make your Scrolldown
function:
...这将使您的Scrolldown
功能:
function Scrolldown() {
window.location.hash = '#iWantToScrollHere';
}
回答by JqueryToAddNumbers
you could include jquery:
你可以包括jquery:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
and then wrap your scrolling thing in a document ready function:
然后将您的滚动内容包装在文档就绪功能中:
$(document).ready(function(){
window.scroll(0,300);
});
then after that you don't need to do anything.
然后在那之后你不需要做任何事情。
:)
:)
回答by mindlogixtech
well, another super simple script is here,
好吧,另一个超级简单的脚本在这里,
<script type="text/javascript">
window.onload = function(e){
window.scrollBy(1,1);
}
it worked best for me.
它最适合我。