Scroll to the top of the page using JavaScript?

后端 未结 30 1035
借酒劲吻你
借酒劲吻你 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:37

    With window.scrollTo(0, 0); is very fast
    so i tried the Mark Ursino example, but in Chrome nothing happens
    and i found this

    $('.showPeriodMsgPopup').click(function(){
        //window.scrollTo(0, 0);
        $('html').animate({scrollTop:0}, 'slow');//IE, FF
        $('body').animate({scrollTop:0}, 'slow');//chrome, don't know if Safari works
        $('.popupPeriod').fadeIn(1000, function(){
            setTimeout(function(){$('.popupPeriod').fadeOut(2000);}, 3000);
        });
    });
    

    tested all 3 browsers and it works
    i'm using blueprint css
    this is when a client clicks "Book now" button and doesn't have the rental period selected, slowly moves to the top where the calendars are and opens a dialog div pointing to the 2 fields, after 3sec it fades

    0 讨论(0)
  • 2020-11-22 03:37

    Why don't you use JQuery inbuilt function scrollTop :

    $('html, body').scrollTop(0);//For scrolling to top
    
    $("body").scrollTop($("body")[0].scrollHeight);//For scrolling to bottom
    

    Short and simple!

    0 讨论(0)
  • 2020-11-22 03:39

    Try this to scroll on top

    <script>
     $(document).ready(function(){
        $(window).scrollTop(0);
    });
    </script>
    
    0 讨论(0)
  • 2020-11-22 03:39

    All of these suggestions work great for various situations. For those who find this page through a search, one can also give this a try. JQuery, no plug-in, scroll to element.

    $('html, body').animate({
        scrollTop: $("#elementID").offset().top
    }, 2000);
    
    0 讨论(0)
  • 2020-11-22 03:39

    Non-jQuery solution / pure JavaScript:

    document.body.scrollTop = document.documentElement.scrollTop = 0;
    
    0 讨论(0)
  • 2020-11-22 03:41
    <script>
    
      $("a[href='#top']").click(function() {
         $("html, body").animate({ scrollTop: 0 }, "slow");
         return false;
      });
    </script>
    

    in html

    <a href="#top">go top</a>
    
    0 讨论(0)
提交回复
热议问题