jQuery move to anchor location on page load

前端 未结 3 1082
星月不相逢
星月不相逢 2021-01-30 06:22

I have a simple page setup such as:

About us content...
Header content...
相关标签:
3条回答
  • 2021-01-30 06:46

    Put this right before the closing Body tag at the bottom of the page.

    <script>
        if (location.hash) {
            location.href = location.hash;
        }
    </script>
    

    jQuery is actually not required.

    0 讨论(0)
  • 2021-01-30 06:47

    Did you tried JQuery's scrollTo method? http://demos.flesler.com/jquery/scrollTo/

    Or you can extend JQuery and add your custom mentod:

    jQuery.fn.extend({
     scrollToMe: function () {
       var x = jQuery(this).offset().top - 100;
       jQuery('html,body').animate({scrollTop: x}, 400);
    }});
    

    Then you can call this method like:

    $("#header").scrollToMe();
    
    0 讨论(0)
  • 2021-01-30 06:55

    Description

    You can do this using jQuery's .scrollTop() and .offset() method

    Check out my sample and this jsFiddle Demonstration

    Sample

    $(function() {
        $(document).scrollTop( $("#header").offset().top );  
    });
    

    More Information

    • jsFiddle Demonstration
    • jQuery.scrollTop()
    • jQuery.offset()
    0 讨论(0)
提交回复
热议问题