I have a basic script that scrolls smoothly to anchored links when the user clicks on them. My problem is that the smooth scroll only seems to work one way, when I click on one of the "go to top" links, the page just jumps...
$(function(){
// Smooth scrolling for anchored links
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 2000);
return false;
}
}
});
// Scroll down automatically on page load
$('html, body').animate({
scrollTop: $('.destination').offset().top
}, 2000);
});
https://jsfiddle.net/p70d4doq/22/
Any help would be much appreciated!
Thanks, Andreas
You forgot to add this bit:
$('a[href="#"]').click(function () {
$('html, body').animate({
scrollTop: 0
}, 2000);
});
Please do not give a fiddle that doesn't work! Check before posting links.
Working Fiddle: https://jsfiddle.net/a5u0zzLr/