How to scroll to top when a button is clicked?

后端 未结 3 744
暖寄归人
暖寄归人 2021-02-05 22:52

I have a button on a page and I want that when it is clicked, the page scrolls to the 0 position.

HTML

相关标签:
3条回答
  • 2021-02-05 23:13

    you can use window function for scrollTop jquery

     $("#button").click( function() {
       $(window).scrollTop(0);
     });
    

    When you click button, this page scroll to top of the position.

    0 讨论(0)
  • 2021-02-05 23:26

    Try It

    $("#button").click(function() {
            $("html").scrollTop(0);
            });
    

    or

    $("#button").click(function() {
            $("html").animate({ scrollTop: 0 }, "slow");
            });
    
    0 讨论(0)
  • 2021-02-05 23:28

    Try this code:

    $("#button").on("click", function() {
        $("body").scrollTop(0);
    });
    

    Method on binds a click event to the button and scrollTop scrolls your body to 0 position.

    0 讨论(0)
提交回复
热议问题