I've been trying to implement a smooth scroll to top using the this CSS-Tricks Snippet.
Now I have the following HTML:
<!-- At the top of the page -->
<p id="content_jump" class="hide">
<a id="#top"></a>
</p>
<!-- A bunch of content -->
<!-- At the bottom of the page -->
<a rel="nofollow" href="#top" id="backtotop" title="Go to top" onclick="scrollToTop();">
Back to Top
</a>
Then I have the following script:
function scrollToTop() {
$('html, body').animate({
scrollTop: $("#top").offset().top
}, 1000};
return false;
}
I expect this to smoothly scroll to the top of the page over the period of 1 second.
However, when I click "Back to Top" it instantly goes to the top and the console provides me with the following error:
Uncaught TypeError: Cannot read property 'top' of undefined at scrollToTop ((index):274) at HTMLAnchorElement.onclick (VM1390 :245)
Any idea on a solution to the problem would be much appreciated.
idoftop. If you want to go to the top of the page just usescrollTop: 0