如何使用JavaScript滚动到页面顶部?滚动条立即跳到页面顶部也是可取的,因为我不希望实现平滑滚动。


当前回答

如果您想平滑滚动,请尝试以下操作:

$("a").click(function() {
     $("html, body").animate({ scrollTop: 0 }, "slow");
     return false;
});

另一种解决方案是JavaScript window.scrollTo方法:

 window.scrollTo(x-value, y-value);

参数:

x值是沿水平轴的像素。y值是沿垂直轴的像素。

其他回答

function scrolltop() {

    var offset = 220;
    var duration = 500;

    jQuery(window).scroll(function() {
        if (jQuery(this).scrollTop() > offset) {
            jQuery('#back-to-top').fadeIn(duration);
        } else {
            jQuery('#back-to-top').fadeOut(duration);
        }
    });

    jQuery('#back-to-top').click(function(event) {
        event.preventDefault();
        jQuery('html, body').animate({scrollTop: 0}, duration);
        return false;
    });
}

以上答案在SharePoint 2016中都不起作用。

必须这样做:https://sharepoint.stackexchange.com/questions/195870/

var w = document.getElementById("s4-workspace");
w.scrollTop = 0;
document.getElementsByTagName('html')[0].scrollIntoView({ behavior: "smooth" });

document.getElementById(“elem”)scrollIntoView();

$(文档).sollTop(0);同样有效。