Uncaught Typeerror: $(...). Animate is not a Function

Asked

Viewed 423 times

-2

If it was noon and I could not find the problem, a fact that I thought was simple. The Jquery documentation contains: $( "#book" ).({

I’ve tried adding a newer, older Jquery, but it won’t. A simple scroll script that I need to implement does not work.

function scrollToAnchor(aid) {
  var aTag = $("a[name='" + aid + "']");
  $('html,body').animate({
    scrollTop: aTag.offsetTop
  }, 'slow');
}

$("#quem-somos").on('click', function() {
  scrollToAnchor('section-quem-somos');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a class="menu__item" href="#" id="quem-somos">
  <span class="menu__item-name">Quem somos</span>
</a>
<section class="bloco_historia" id="section-quem-somos">Bloco</section>

He’s near the baseboard. If I switch to the head, it’s no mistake, but it doesn’t work either.

  • 1

    Why in jQuery you search for the tag <a> that has the attribute name where its element does not possess such an attribute? Moreover, neither an element <a> he is, is a <section> who owns the id.

  • Got it. But I changed the name to id, but nothing yet.

  • And changed the a for section?

1 answer

0

You will need to change some of your code to make it work. In Javascript I suggest doing something like this:

function scrollToAnchor(sectionID) {
  $('html,body').animate({
    scrollTop: $('#section-' + sectionID).offset().top
  }, 'slow');
}

$("a.menu__item").on('click', function() {
  var linkID= $(this).attr('id');
  scrollToAnchor(linkID);
});

Detail of the change:

To function scrollToAnchor receives the ID of the <section> as a parameter and in the .animate() picks up the offset via the widget selector that needs to be displayed by scroll.

Notice that the selector is the link/menu ID with prefix Section, soon, 'section-' + sectionID stays 'section-quem-somos'.

And in the click, the selector for the event becomes more generic, covering only the menu context elements, providing you can include new links. Already the Handle event call the function scrollToAnchor() passing as parameter a string of the ID itself of the clicked element.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.