jQuery 如何在页面加载时自动将窗口滚动到某个高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18482035/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:43:44  来源:igfitidea点击:

How to automatically scroll the window to some height as the page loads

jquery

提问by Thakur Saksham Chauhan

I want to automatically scroll the page to 120px as the page loads please help.

我想在页面加载时自动将页面滚动到 120 像素,请帮忙。

I am new to Javascript and jQuery so please use easy javascript or jQuery (And P.S. I'm familier with the "function" thing in javascript).

我是 Javascript 和 jQuery 的新手,所以请使用简单的 javascript 或 jQuery(而且 PS 我对 javascript 中的“函数”事物很熟悉)。

THANX.

谢谢。

回答by S. S. Rawat

Demo Here

演示 Here

Code $("html, body").animate({ scrollTop: 1000 }, 2000);

代码 $("html, body").animate({ scrollTop: 1000 }, 2000);

回答by Suresh Atta

$("body").animate({ scrollTop: 120 }, "slow");

Working Demo

工作演示

Make sure that,You write the above line in document.ready()function

确保,你在document.ready()函数中写了上面的行

回答by Tushar Gupta - curioustushar

JavaScript approach

JavaScript 方法

DEMOby s???s? ????

演示s???s? ???

document.body.scrollTop = 1000;

回答by Mohamed Khamis

If you are scrolling to a particular element with id element-id, you can use this:

如果要滚动到带有 id 的特定元素element-id,则可以使用以下命令:

document.getElementById("element-id").scrollIntoView();