php Ajax 内容加载后滚动到顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9777563/
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 top after Ajax content load
提问by Aaron Fisher
Is there anyway that I can make it so the page will automatically scroll to the top after the content has loaded (via Ajax)?
无论如何我可以使它在内容加载后页面会自动滚动到顶部(通过 Ajax)?
This is the code I have for displaying the content:
这是我用于显示内容的代码:
$(document).ready(function () {
var my_layout = $('#container').layout();
$("a.item_link").click(function () {
$("#loader").fadeIn();
feed_url = $(this).attr("href");
$.ajax({
type: "POST",
data: "URL=" + feed_url,
url: "view.php",
success: function (msg) {
$("#view-area").html(msg);
$("#loader").fadeOut();
}
});
return false;
});
});
So after the 'view-area' has loaded its content can I make the page auto scroll to the top?
那么在'view-area'加载其内容后,我可以让页面自动滚动到顶部吗?
回答by Justin Pihony
Just use the scroll function
只需使用滚动功能
scrollTo(0);
If you want the jquery, then here is a good example with smoothing:)
如果你想要 jquery,那么这里是一个很好的平滑例子:)
From the link:
从链接:
$('html, body').animate({ scrollTop: 0 }, 0);
//nice and slow :)
$('html, body').animate({ scrollTop: 0 }, 'slow');
To put it in your code
把它放在你的代码中
...
success: function (msg) {
$("#view-area").html(msg);
$("#loader").fadeOut();
//Put code here like so
$('html, body').animate({ scrollTop: 0 }, 0);
}
回答by Icarus
You can do $(window).scrollTop(0);
你可以做 $(window).scrollTop(0);
回答by PaulKoeck
All ajax requests have a callback argument so use scrollTop(0)
.
Check the jQuery documentation on how to use ajax callbacks.
所有 ajax 请求都有一个回调参数,所以使用scrollTop(0)
. 查看有关如何使用 ajax 回调的 jQuery 文档。