Page Scroll with jQuery

Asked

Viewed 157 times

0

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

Uncaught Typeerror: Cannot 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 don’t understand, I’m a layman with javascript the code was from a video I saw on Youtube.

1 answer

1


Hello friend tries to check the version of jquery you are using, I mounted one that picks up 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>

  • Thanks, I had even given up, was the version of jquery same.

Browser other questions tagged

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