-2

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

Andreas
  • 21
  • 1
  • 4

1 Answers1

0

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/

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    Ok, I'm sorry. I asked a question here earlier today and someone asked me to post a fiddle of code I had problems with. So I thought it would be ok to do this. Thanks anyway, I appreciate your help! – Andreas Sep 19 '15 at 14:49