Scroll to specific div on page load

后端 未结 3 661
长发绾君心
长发绾君心 2020-12-24 02:34

I\'m trying to figure out how get the page automaticlly scroll to a specific div when the page has loaded. I have tried using the jQuery scroll function, but cant get it to

相关标签:
3条回答
  • 2020-12-24 02:57
    $(document).ready(function(){
        $("html, body").animate({ 
            scrollTop: $('.sb-menu').offset().top 
        }, 1000);
    });
    
    0 讨论(0)
  • 2020-12-24 02:58

    You can do this using the .animate() method:

    $(document).ready(function () {
        // Handler for .ready() called.
        $('html, body').animate({
            scrollTop: $('#what').offset().top
        }, 'slow');
    });
    
    • This will smooth scroll to the div with ID what

    FIDDLE

    0 讨论(0)
  • 2020-12-24 03:17

    Firsts you have to call the file,

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
    

    Here the id is 'scroll'. The following code is helpful:

    <html>
    <head>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript">
    
    $(document).ready(function () {
        // Handler for .ready() called.
        $('html, body').animate({
            scrollTop: $('#scroll').offset().top
        }, 'slow');
    });
    
    </script>
    
    </head>
    <body>
    
    <div id="scroll"></div>
    
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题