0

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 work correctly. Any suggestions?

  • 7
    Possible duplicate of [Scroll to a div using jquery](http://stackoverflow.com/questions/3432656/scroll-to-a-div-using-jquery) – Anupam Jun 03 '16 at 07:26
  • 1
    duplicate of [javascript-to-scroll-long-page-to-div](http://stackoverflow.com/q/68165/456135) – Anupam Jun 03 '16 at 07:28
  • I would suggest Bootstraps scrollspy, http://getbootstrap.com/javascript/#scrollspy it is very easy to implement. – Jesper Højer Jun 03 '16 at 07:30
  • Try this similar post: [enter link description here](http://stackoverflow.com/questions/5007530/how-do-i-scroll-to-an-element-using-javascript) – rjay dadula Jun 03 '16 at 08:22
  • [http://stackoverflow.com/questions/5007530/how-do-i-scroll-to-an-element-using-javascript](http://stackoverflow.com/questions/5007530/how-do-i-scroll-to-an-element-using-javascript) – rjay dadula Jun 03 '16 at 08:24

2 Answers2

1

I did a quick search for you, would something like this fit your needs?

$(function() {
  $('a[href*="#"]:not([href="#"])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

Found here: https://css-tricks.com/snippets/jquery/smooth-scrolling/

You can test it out there as well!

Wgreen92
  • 11
  • 4
-1

You may try something like below in jQuery:-

$('html,body').animate({
    scrollTop: $(".targetDiv").offset().top},
'slow');
Vivek Pratap Singh
  • 9,326
  • 5
  • 21
  • 34