Uncaught TypeError: $ (...) .animate is not a function

-2

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

I have tried to put a newer, older Jquery, but, it will not. It does not work a simple scroll script that I need to implement.

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><aclass="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>

It's close to the footer. If I switch to the head, it does not make a mistake, but it does not work either.

    
asked by anonymous 27.09.2018 / 20:45

1 answer

0

You'll need to change some of your code to 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);
});

Change Detail:

The function scrollToAnchor gets the ID of the <section> as a parameter and in .animate() it takes the offset via the element selector that needs to be displayed by scroll .

Notice that the selector is the join of the ID of the link / menu with the prefix section , so, 'section-' + sectionID gets 'section-quem-somos' .

And click , the selector for the event becomes more generic, covering only context menu elements, allowing you to add new links. The handle of the event calls the function scrollToAnchor() passing as a parameter a string of the element ID itself.

    
24.10.2018 / 19:55