【发布时间】:2019-05-08 23:33:43
【问题描述】:
我试图弄清楚当页面加载后如何让页面自动滚动到特定的 div。我曾尝试使用 jQuery 滚动功能,但无法使其正常工作。 有什么建议吗?
以下是我迄今为止尝试过的:
jQuery(function() {
jQuery(window).scrollTop(jQuery('.container').offset().top);
});
【问题讨论】:
我试图弄清楚当页面加载后如何让页面自动滚动到特定的 div。我曾尝试使用 jQuery 滚动功能,但无法使其正常工作。 有什么建议吗?
以下是我迄今为止尝试过的:
jQuery(function() {
jQuery(window).scrollTop(jQuery('.container').offset().top);
});
【问题讨论】:
您可以使用.animate() 方法做到这一点:
$(document).ready(function () {
// Handler for .ready() called.
$('html, body').animate({
scrollTop: $('#what').offset().top
}, 'slow');
});
what 的 div
【讨论】:
$('#' + divToScrollTo).offset().top,其中divToScrollTo 是我们存储查询字符串值的 js 变量。
$(document).ready(function(){
$("html, body").animate({
scrollTop: $('.sb-menu').offset().top
}, 1000);
});
【讨论】:
首先你必须调用文件,
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
这里的 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>
【讨论】: