• " />
  • " />
  • "/>

    Simple jQuery scrollTo will not work/animate

    83 views Asked by At

    I have a fixed header bar with basic navigation like so:

    <html>
    <head></head>
    <body>
    <ul class="nav navbar-nav">
      <li><a id="aboutme" href="#about-me">About</a></li>
      <li><a id="workme" href="#work-me">Work</a></li>
      <li><a id="contactme" href="#contact-me">Contact</a></li>
    </ul>
    ...
    ..
    .
    <div id="about-me">
    </div>
    </body>
    </html>
    

    I want these to direct to a section in the same page using the 'linear', 'slow' animation values, but my code isn't working:

    $(document).ready(function() {
    
      $('aboutme').click(function() {
        $.scrollTo($("#about-me"),{duration: 1});
      });
    });
    

    Do you know where my syntax has gone wrong? And would I need to use the jQuery animatedScroll on github to achieve this?

    Thanks :)

    2

    There are 2 answers

    0
    adeneo On

    Anyway, here's how to animate the scrolling without a plugin ?

    $(document).ready(function() {
        $('#aboutme').click(function(e) {
           e.preventDefault();
           $('body, html').animate({scrollTop : $('#about-me').offset().top}, 'slow');
        });
    });
    
    0
    DaniP On

    I guess you are using this ScrollTo plugin.

    In your function first reference well your button, you have:

    $('aboutme')

    must be

    $('#aboutme')

    Then set the time value with just a , like this:

    $('#aboutme').click(function(e) {
      e.preventDefault();
      $.scrollTo($("#about-me"),800);
    });
    

    Check this Demo Fiddle