如何使用 JavaScript/ jQuery 滚动到页面顶部?


示例

实时演示

<html>
   <body>
      <script>
         $("a").click(function() {
            $("html, body").animate({ scrollTop: 0 }, "slow");
            return false;
         });
      </script>
      <h3>TOP OF PAGE</h3>
      <br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.<br>
      This is demo text.
      <br>
      <h3>BOTTOM OF PAGE</h3>
      <a href="#">Scroll up</a>
   </body>
</html>

更新于: 16-6-2020

201 次浏览

开启你的职业生涯

通过完成课程获得认证

开始
广告