jQuery 在页面加载时滚动到特定的 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18103534/
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
Scroll to specific div on page load
提问by simon
I'm trying to figure out how get the page automaticlly scroll to a specific div when the page has loaded. I have tried using the jQuery scroll function, but cant get it to work correctly. Any suggestions?
我试图弄清楚如何在页面加载后让页面自动滚动到特定的 div。我曾尝试使用 jQuery 滚动功能,但无法使其正常工作。有什么建议?
Following is what i have tried so far:
以下是我迄今为止尝试过的:
jQuery(function() {
jQuery(window).scrollTop(jQuery('.container').offset().top);
});
回答by pala?н
You can do this using the .animate()
method:
您可以使用以下.animate()
方法执行此操作:
$(document).ready(function () {
// Handler for .ready() called.
$('html, body').animate({
scrollTop: $('#what').offset().top
}, 'slow');
});
- This will smooth scroll to the div with ID
what
- 这将平滑滚动到带有 ID 的 div
what
回答by JWC May
Firsts you have to call the file,
首先你必须调用文件,
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
Here the id is 'scroll'. The following code is helpful:
这里的 id 是“滚动”。以下代码很有帮助:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// Handler for .ready() called.
$('html, body').animate({
scrollTop: $('#scroll').offset().top
}, 'slow');
});
</script>
</head>
<body>
<div id="scroll"></div>
</body>
</html>
回答by tailor
$(document).ready(function(){
$("html, body").animate({
scrollTop: $('.sb-menu').offset().top
}, 1000);
});