scrollTop jquery, scrolling to div with id?









up vote
31
down vote

favorite
9












So this is the current code I have



$(document).ready(function() 
$('.abouta').click(function()
$('html, body').animate(scrollTop:308, 'slow');
return false;
);
$('.portfolioa').click(function()
$('html, body').animate(scrollTop:708, 'slow');
return false;
);
$('.contacta').click(function()
$('html, body').animate(scrollTop:1108, 'slow');
return false;
);
);


When you click a link e.g. 'abouta' it scrolls to that specific section of the page. I'd rather do a scrollTo and then the id of a div so that I don't have to keep changing the scrollTo position if I change padding etc. Is there any way?
Thanks










share|improve this question



























    up vote
    31
    down vote

    favorite
    9












    So this is the current code I have



    $(document).ready(function() 
    $('.abouta').click(function()
    $('html, body').animate(scrollTop:308, 'slow');
    return false;
    );
    $('.portfolioa').click(function()
    $('html, body').animate(scrollTop:708, 'slow');
    return false;
    );
    $('.contacta').click(function()
    $('html, body').animate(scrollTop:1108, 'slow');
    return false;
    );
    );


    When you click a link e.g. 'abouta' it scrolls to that specific section of the page. I'd rather do a scrollTo and then the id of a div so that I don't have to keep changing the scrollTo position if I change padding etc. Is there any way?
    Thanks










    share|improve this question

























      up vote
      31
      down vote

      favorite
      9









      up vote
      31
      down vote

      favorite
      9






      9





      So this is the current code I have



      $(document).ready(function() 
      $('.abouta').click(function()
      $('html, body').animate(scrollTop:308, 'slow');
      return false;
      );
      $('.portfolioa').click(function()
      $('html, body').animate(scrollTop:708, 'slow');
      return false;
      );
      $('.contacta').click(function()
      $('html, body').animate(scrollTop:1108, 'slow');
      return false;
      );
      );


      When you click a link e.g. 'abouta' it scrolls to that specific section of the page. I'd rather do a scrollTo and then the id of a div so that I don't have to keep changing the scrollTo position if I change padding etc. Is there any way?
      Thanks










      share|improve this question















      So this is the current code I have



      $(document).ready(function() 
      $('.abouta').click(function()
      $('html, body').animate(scrollTop:308, 'slow');
      return false;
      );
      $('.portfolioa').click(function()
      $('html, body').animate(scrollTop:708, 'slow');
      return false;
      );
      $('.contacta').click(function()
      $('html, body').animate(scrollTop:1108, 'slow');
      return false;
      );
      );


      When you click a link e.g. 'abouta' it scrolls to that specific section of the page. I'd rather do a scrollTo and then the id of a div so that I don't have to keep changing the scrollTo position if I change padding etc. Is there any way?
      Thanks







      javascript jquery css scroll position






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 29 '11 at 20:52









      jondavidjohn

      50.1k1898143




      50.1k1898143










      asked Dec 30 '10 at 18:57









      Jake

      5243814




      5243814






















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          100
          down vote



          accepted










          instead of



          $('html, body').animate(scrollTop:xxx, 'slow');


          use



          $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');


          this will return the absolute top position of whatever element you select as #div_id






          share|improve this answer


















          • 1




            Works perfectly, cheers :D
            – Jake
            Dec 30 '10 at 21:40






          • 3




            I am surprised no one has commented in so long - the question as it stands, really asks for .offset() rather than .position() as a solution, right? The target div could be anywhere on the page, and .position() returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
            – sameers
            Jul 22 '16 at 1:54










          • Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
            – Dan382
            Sep 24 '17 at 19:33

















          up vote
          0
          down vote













          try this:



          $('html, body').animate(scrollTop:$('#xxx').position().top, 'slow');
          $('#xxx').focus();





          share|improve this answer



























            up vote
            0
            down vote













            My solution was the following:



            document.getElementById("agent_details").scrollIntoView();





            share|improve this answer





























              up vote
              -2
              down vote













              if you want to scroll just only some div, can use the div id instead of 'html, body'



              $('html, body').animate(...


              use



              $('#mydivid').animate(...





              share|improve this answer
















              • 3




                Nope, $('#div').animate(scrollTop:$('#div').position().top, 'slow'); doesn't work. (Chrome 23). Better use $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
                – bicycle
                Dec 14 '12 at 17:48


















              up vote
              -2
              down vote













              try this



               $('#div_id').animate(scrollTop:0, '500', 'swing');





              share|improve this answer




















                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4565381%2fscrolltop-jquery-scrolling-to-div-with-id%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                100
                down vote



                accepted










                instead of



                $('html, body').animate(scrollTop:xxx, 'slow');


                use



                $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');


                this will return the absolute top position of whatever element you select as #div_id






                share|improve this answer


















                • 1




                  Works perfectly, cheers :D
                  – Jake
                  Dec 30 '10 at 21:40






                • 3




                  I am surprised no one has commented in so long - the question as it stands, really asks for .offset() rather than .position() as a solution, right? The target div could be anywhere on the page, and .position() returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
                  – sameers
                  Jul 22 '16 at 1:54










                • Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
                  – Dan382
                  Sep 24 '17 at 19:33














                up vote
                100
                down vote



                accepted










                instead of



                $('html, body').animate(scrollTop:xxx, 'slow');


                use



                $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');


                this will return the absolute top position of whatever element you select as #div_id






                share|improve this answer


















                • 1




                  Works perfectly, cheers :D
                  – Jake
                  Dec 30 '10 at 21:40






                • 3




                  I am surprised no one has commented in so long - the question as it stands, really asks for .offset() rather than .position() as a solution, right? The target div could be anywhere on the page, and .position() returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
                  – sameers
                  Jul 22 '16 at 1:54










                • Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
                  – Dan382
                  Sep 24 '17 at 19:33












                up vote
                100
                down vote



                accepted







                up vote
                100
                down vote



                accepted






                instead of



                $('html, body').animate(scrollTop:xxx, 'slow');


                use



                $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');


                this will return the absolute top position of whatever element you select as #div_id






                share|improve this answer














                instead of



                $('html, body').animate(scrollTop:xxx, 'slow');


                use



                $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');


                this will return the absolute top position of whatever element you select as #div_id







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 15 '11 at 3:56

























                answered Dec 30 '10 at 19:05









                jondavidjohn

                50.1k1898143




                50.1k1898143







                • 1




                  Works perfectly, cheers :D
                  – Jake
                  Dec 30 '10 at 21:40






                • 3




                  I am surprised no one has commented in so long - the question as it stands, really asks for .offset() rather than .position() as a solution, right? The target div could be anywhere on the page, and .position() returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
                  – sameers
                  Jul 22 '16 at 1:54










                • Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
                  – Dan382
                  Sep 24 '17 at 19:33












                • 1




                  Works perfectly, cheers :D
                  – Jake
                  Dec 30 '10 at 21:40






                • 3




                  I am surprised no one has commented in so long - the question as it stands, really asks for .offset() rather than .position() as a solution, right? The target div could be anywhere on the page, and .position() returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
                  – sameers
                  Jul 22 '16 at 1:54










                • Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
                  – Dan382
                  Sep 24 '17 at 19:33







                1




                1




                Works perfectly, cheers :D
                – Jake
                Dec 30 '10 at 21:40




                Works perfectly, cheers :D
                – Jake
                Dec 30 '10 at 21:40




                3




                3




                I am surprised no one has commented in so long - the question as it stands, really asks for .offset() rather than .position() as a solution, right? The target div could be anywhere on the page, and .position() returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
                – sameers
                Jul 22 '16 at 1:54




                I am surprised no one has commented in so long - the question as it stands, really asks for .offset() rather than .position() as a solution, right? The target div could be anywhere on the page, and .position() returns the top offset relative to its container box. So the above code will take you to the top of your document, if the target div is a first child of its parent. Or am I missing something?
                – sameers
                Jul 22 '16 at 1:54












                Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
                – Dan382
                Sep 24 '17 at 19:33




                Thanks Sameers, thats exactly the issue I was having. .offset() works a treat.
                – Dan382
                Sep 24 '17 at 19:33












                up vote
                0
                down vote













                try this:



                $('html, body').animate(scrollTop:$('#xxx').position().top, 'slow');
                $('#xxx').focus();





                share|improve this answer
























                  up vote
                  0
                  down vote













                  try this:



                  $('html, body').animate(scrollTop:$('#xxx').position().top, 'slow');
                  $('#xxx').focus();





                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    try this:



                    $('html, body').animate(scrollTop:$('#xxx').position().top, 'slow');
                    $('#xxx').focus();





                    share|improve this answer












                    try this:



                    $('html, body').animate(scrollTop:$('#xxx').position().top, 'slow');
                    $('#xxx').focus();






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 14 '14 at 15:33









                    PauLo Guajardo Torrealba

                    823




                    823




















                        up vote
                        0
                        down vote













                        My solution was the following:



                        document.getElementById("agent_details").scrollIntoView();





                        share|improve this answer


























                          up vote
                          0
                          down vote













                          My solution was the following:



                          document.getElementById("agent_details").scrollIntoView();





                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            My solution was the following:



                            document.getElementById("agent_details").scrollIntoView();





                            share|improve this answer














                            My solution was the following:



                            document.getElementById("agent_details").scrollIntoView();






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 11 at 9:10









                            DaFois

                            1,74031418




                            1,74031418










                            answered Nov 11 at 8:16









                            Alexis Cabrera Mondeja

                            61




                            61




















                                up vote
                                -2
                                down vote













                                if you want to scroll just only some div, can use the div id instead of 'html, body'



                                $('html, body').animate(...


                                use



                                $('#mydivid').animate(...





                                share|improve this answer
















                                • 3




                                  Nope, $('#div').animate(scrollTop:$('#div').position().top, 'slow'); doesn't work. (Chrome 23). Better use $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
                                  – bicycle
                                  Dec 14 '12 at 17:48















                                up vote
                                -2
                                down vote













                                if you want to scroll just only some div, can use the div id instead of 'html, body'



                                $('html, body').animate(...


                                use



                                $('#mydivid').animate(...





                                share|improve this answer
















                                • 3




                                  Nope, $('#div').animate(scrollTop:$('#div').position().top, 'slow'); doesn't work. (Chrome 23). Better use $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
                                  – bicycle
                                  Dec 14 '12 at 17:48













                                up vote
                                -2
                                down vote










                                up vote
                                -2
                                down vote









                                if you want to scroll just only some div, can use the div id instead of 'html, body'



                                $('html, body').animate(...


                                use



                                $('#mydivid').animate(...





                                share|improve this answer












                                if you want to scroll just only some div, can use the div id instead of 'html, body'



                                $('html, body').animate(...


                                use



                                $('#mydivid').animate(...






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jul 5 '12 at 5:52









                                Fawas

                                132




                                132







                                • 3




                                  Nope, $('#div').animate(scrollTop:$('#div').position().top, 'slow'); doesn't work. (Chrome 23). Better use $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
                                  – bicycle
                                  Dec 14 '12 at 17:48













                                • 3




                                  Nope, $('#div').animate(scrollTop:$('#div').position().top, 'slow'); doesn't work. (Chrome 23). Better use $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
                                  – bicycle
                                  Dec 14 '12 at 17:48








                                3




                                3




                                Nope, $('#div').animate(scrollTop:$('#div').position().top, 'slow'); doesn't work. (Chrome 23). Better use $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
                                – bicycle
                                Dec 14 '12 at 17:48





                                Nope, $('#div').animate(scrollTop:$('#div').position().top, 'slow'); doesn't work. (Chrome 23). Better use $('html, body').animate(scrollTop:$('#div_id').position().top, 'slow');
                                – bicycle
                                Dec 14 '12 at 17:48











                                up vote
                                -2
                                down vote













                                try this



                                 $('#div_id').animate(scrollTop:0, '500', 'swing');





                                share|improve this answer
























                                  up vote
                                  -2
                                  down vote













                                  try this



                                   $('#div_id').animate(scrollTop:0, '500', 'swing');





                                  share|improve this answer






















                                    up vote
                                    -2
                                    down vote










                                    up vote
                                    -2
                                    down vote









                                    try this



                                     $('#div_id').animate(scrollTop:0, '500', 'swing');





                                    share|improve this answer












                                    try this



                                     $('#div_id').animate(scrollTop:0, '500', 'swing');






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Feb 26 '15 at 9:17









                                    pirate

                                    1




                                    1



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4565381%2fscrolltop-jquery-scrolling-to-div-with-id%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        這個網誌中的熱門文章

                                        How to read a connectionString WITH PROVIDER in .NET Core?

                                        Node.js Script on GitHub Pages or Amazon S3

                                        Museum of Modern and Contemporary Art of Trento and Rovereto