JQuery滚动到页面顶部
问题描述:
有没有一种方法来编程滚动到页面的顶部与jQuery?我目前正试图用下面的代码来做到这一点,但它不工作。我使用的是Firefox目前,JQuery滚动到页面顶部
$(window).scrollTop($(document).height());
答
试试这个:
$('html, body').animate({scrollTop: '0px'}, 300);
为此,您可以用0
而不是300
是即时的,但这样就可以快速自动滚动效果。
答
只需添加一个片段给尼克的很好的答案。这仅在用户向下滚动页面(即,Pinterest风格)之后才显示“滚动到顶部”元素。
$("#scroll_to_top_button").hide(); // hide on page load
$(window).bind('scroll', function(){
if($(this).scrollTop() > 200) { // show after 200 px of user scrolling
$("#scroll_to_top").slideDown("fast");
}
});
答
这里不需要jQuery。使用原生:
window.scrollTo(x-coordinate, y-coordinate);
注意,这已超过动画部分(持续时间等)jQuery提供
答
$('a[href^="#"]').on('click', function(event) {
var target = $(this.getAttribute('href'));
if(target.length) {
event.preventDefault();
$('html, body').stop().animate({
scrollTop: target.offset().top
}, 1000);
}
})无法控制的;
一下添加到HTML
<div id="top"></div>
<a href="#top">Click to scroll up</a>
我想jQueryUI的对话框控件滚动到在发射前,这正是我一直在寻找。谢谢。 – 2012-02-15 08:25:14