0

The animate doesn't work, can you tell me what I'm doing wrong?

<a id="pediatric-primary-health-care" href="#" class="anchor">&nbsp;</a>

<script>
    jQuery(document).ready(function() {
        function scrollToAnchor(aid){
            var aTag = jQuery("a[name='"+ aid +"']");
            jQuery('html,body').animate({
                scrollTop: aTag.offset().top
            }, 'slow');
        }
        jQuery("a").click(function() {
            var href = jQuery(this).attr('href').replace('#', '')
            scrollToAnchor(href);
        });
    });
</script>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Colette
  • 23
  • 3

1 Answers1

0

The syntax of the JavaScript itself is wrong. You have:

jQuery('html,body').animate({
    jQuery('html,body').animate({scrollTop: aTag.offset().top},'slow');

While the animate function just expects an object with CSS properties. Get rid of the first one. Atleast now people should understand the importance of indenting the code.

var aTag = jQuery("a[name='" + aid + "']");
jQuery('html,body').animate({
  scrollTop: aTag.offset().top
}, 'slow');

Also, to not get the # at the end of the URL, you can use event.preventDefault():

jQuery("a").click(function(e) {
    e.preventDefault();
    scrollToAnchor($(this));
});
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252