强制滚动到顶部更新

时间:2015-02-26 10:31:19

标签: javascript jquery html scrolltop

我试图强制我的页面始终加载到顶部。我不知道是否必须定义刷新或负载,但我已尝试过所有内容。我已经搜索过所有以前的问题了,其中没有一个问题似乎适合我。我检查了Chrome和Mozilla。所以你有什么建议吗?我该怎么办?

我尝试了以下内容:

1

$(document).ready(function(){
    $(window).scrollTop(0);
});

2

jQuery('html,body').animate({scrollTop:0},0);

3

$(window).on('beforeunload', function(){
  $(window).scrollTop(0);
});

4

$(document).ready(function() {
    if (window.location.hash) { 
        //bind to scroll function
        $(document).scroll( function() {
            var hash = window.location.hash
            var hashName = hash.substring(1, hash.length);
            var element;

            //if element has this id then scroll to it
            if ($(hash).length != 0) {
                element = $(hash);
            }
            //catch cases of links that use anchor name
            else if ($('a[name="' + hashName + '"]').length != 0)
            {
                //just use the first one in case there are multiples
                element = $('a[name="' + hashName + '"]:first');
            }

            //if we have a target then go to it
            if (element != undefined) {
                window.scrollTo(0, element.position().top);
            }
            //unbind the scroll event
            $(document).unbind("scroll");
        });
    }

});

5

$('a').on('click', function(e) {
 e.preventDefault();
 // do your js stuff
 $(window).scrollTop(0);
});

6

$("html, body").animate({
            scrollTop: 0
}, 600);

1 个答案:

答案 0 :(得分:0)

使用animate功能

$("html, body").animate({
            scrollTop: 0
}, 600);