Page Scroll with jQuery

0

I'm trying to make a page scroll when I click on a link from my navbar, but when I click I get the following error in the console:

  

Uncaught TypeError: Can not read property 'top' of undefined

$(document).ready(function (){
  setBindings();
});


function setBindings() {
  $("nav a").click(function(e) {
    e.preventDefault();
    var sectionID = e.currentTarget.id;
    $('html, body').animate({
        scrollTop: $("#" + sectionID).offset().top
    }, 2000);
});
}

I do not understand, I am a layperson with javascript the code was from a video that I saw on Youtube .

    
asked by anonymous 25.08.2016 / 19:58

1 answer

1

Hello friend, try to check the version of jquery you're using, I've put one that takes the top of the paragraph

$(document).ready(function(){
  $("button").click(function(){
    alert("Top: " + $("p").offset().top);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>    
<p>paragrafo</p>
<button>Clicar</button>
    
29.08.2016 / 16:37