Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
function to scroll to a specific element in the page
//replace #fromA with your button/control and #toB with the target to which you wanna scroll to.
$("#fromA").click(function() {
$("html, body").animate({ scrollTop: $("#toB").offset().top }, 1500);
console.log('executed scrollToElement');
return true;
});
@danharding

This comment has been minimized.

Show comment Hide comment
@danharding

danharding Jan 11, 2013

Thanks, tried heaps of stuff and this is was the quickest and best solution!

Thanks, tried heaps of stuff and this is was the quickest and best solution!

@lorddarq

This comment has been minimized.

Show comment Hide comment
@lorddarq

lorddarq Jan 28, 2014

Glad to hear that :)

Owner

lorddarq commented Jan 28, 2014

Glad to hear that :)

@alucardu

This comment has been minimized.

Show comment Hide comment
@alucardu

alucardu Jun 8, 2014

$("#projects").click(function (e) {
  event.preventDefault();
  $(".container-content").animate({ scrollTop: $(".content-projects").offset().top }, 1500);
});

Gives me this error

Uncaught TypeError: Cannot read property 'top' of undefined 

I have the .content-projects div inside the .container-content.

// Fixed problem

I made a small typo in my HTML doh.

alucardu commented Jun 8, 2014

$("#projects").click(function (e) {
  event.preventDefault();
  $(".container-content").animate({ scrollTop: $(".content-projects").offset().top }, 1500);
});

Gives me this error

Uncaught TypeError: Cannot read property 'top' of undefined 

I have the .content-projects div inside the .container-content.

// Fixed problem

I made a small typo in my HTML doh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment