Scroll to the top of the page using JavaScript?

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

    Better solution with smooth animation:

    // this changes the scrolling behavior to "smooth"
    window.scrollTo({ top: 0, behavior: 'smooth' });
    

    Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo#Example

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

    You can try using JS as in this Fiddle http://jsfiddle.net/5bNmH/1/

    Add the "Go to top" button in your page footer:

    <footer>
        <hr />
        <p>Just some basic footer text.</p>
        <!-- Go to top Button -->
        <a href="#" class="go-top">Go Top</a>
    </footer>
    
    0 讨论(0)
  • 2020-11-22 03:44

    You don't need jQuery to do this. A standard HTML tag will suffice...

    <div id="jump_to_me">
        blah blah blah
    </div>
    
    <a target="#jump_to_me">Click Here To Destroy The World!</a>
    
    0 讨论(0)
  • 2020-11-22 03:45

    A lot of users recommend selecting both the html and body tags for cross-browser compatibility, like so:

    $('html, body').animate({ scrollTop: 0 }, callback);
    

    This can trip you up though if you're counting on your callback running only once. It will in fact run twice because you've selected two elements.

    If that is a problem for you, you can do something like this:

    function scrollToTop(callback) {
        if ($('html').scrollTop()) {
            $('html').animate({ scrollTop: 0 }, callback);
            return;
        }
    
        $('body').animate({ scrollTop: 0 }, callback);
    }
    

    The reason this works is in Chrome $('html').scrollTop() returns 0, but not in other browsers such as Firefox.

    If you don't want to wait for the animation to complete in the case that the scrollbar is already at the top, try this:

    function scrollToTop(callback) {
        if ($('html').scrollTop()) {
            $('html').animate({ scrollTop: 0 }, callback);
            return;
        }
    
        if ($('body').scrollTop()) {
            $('body').animate({ scrollTop: 0 }, callback);
            return;
        }
    
        callback();
    }
    
    0 讨论(0)
  • 2020-11-22 03:45
    function scrolltop() {
    
        var offset = 220;
        var duration = 500;
    
        jQuery(window).scroll(function() {
            if (jQuery(this).scrollTop() > offset) {
                jQuery('#back-to-top').fadeIn(duration);
            } else {
                jQuery('#back-to-top').fadeOut(duration);
            }
        });
    
        jQuery('#back-to-top').click(function(event) {
            event.preventDefault();
            jQuery('html, body').animate({scrollTop: 0}, duration);
            return false;
        });
    }
    
    0 讨论(0)
  • 2020-11-22 03:47

    None of the answers above will work in SharePoint 2016.

    It has to be done like this : https://sharepoint.stackexchange.com/questions/195870/

    var w = document.getElementById("s4-workspace");
    w.scrollTop = 0;
    
    0 讨论(0)
提交回复
热议问题