Scroll to the top of the page using JavaScript?

后端 未结 30 1087
借酒劲吻你
借酒劲吻你 2020-11-22 03:18

How do I scroll to the top of the page using JavaScript? The scrollbar instantly jumping to the top of the page is desirable too as I\'m not looking to achieve smooth scroll

30条回答
  •  时光说笑
    2020-11-22 03:36

    If you want to do smooth scrolling, please try this:

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

    Another solution is JavaScript window.scrollTo method :

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

    Parameters :

    • x-value is the pixel along the horizontal axis.
    • y-value is the pixel along the vertical axis.

提交回复
热议问题