Scroll to the top of the page using JavaScript/jQuery?

I've a <button> on the page, when this button is pressed a hidden <div> is shown using jQuery.

How do I scroll to the top of the page using a JavaScript/jQuery command in that function? It is desirable even if the scroll bar instantly jumps to the top. I'm not looking for a smooth scrolling.


If you don't need the change to animate then you don't need to use any special plugins - I'd just use the native JavaScript window.scrollTo method -- passing in 0,0 will scroll the page to the top left instantly.

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

Parameters

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

  • If you do want smooth scrolling, try something like this:

    $("a[href='#top']").click(function() {
      $("html, body").animate({ scrollTop: 0 }, "slow");
      return false;
    });
    

    That will take any <a> tag whose href="#top" and make it smooth scroll to the top.


    试试这个在顶部滚动

    <script>
     $(document).ready(function(){
        $(window).scrollTop(0);
    });
    </script>
    
    链接地址: http://www.djcxy.com/p/15712.html

    上一篇: 将旧页面重定向到新页面HTML

    下一篇: 使用JavaScript / jQuery滚动到页面顶部?