2

I'm using this code for smooth link scrolling:

    function filterPath(string) {
    return string
      .replace(/^\//,'')
      .replace(/(index|default).[a-zA-Z]{3,4}$/,'')  
      .replace(/\/$/,'');
    }

$('a[href*=#]').each(function() {
if ( filterPath(location.pathname) == filterPath(this.pathname)
&& location.hostname == this.hostname
&& this.hash.replace(/#/,'') ) {
  var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
  var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
   if ($target) {
        $(this).click(function() {
            var targetOffset = $target.offset().top;
            $('html, body, .container-1-2').animate({
                scrollTop: $target.offset().top - 70}, 600
             );

            return false;
        });
  }
}
});

'm scrolling not the whole page, but only certain div with class container-1-2.

The problem I have is that when anchor link is clicked, it scrolls to the id which was specified, but when I click the same link again, it scrolls to the top. Just can't figure it out how to prevent scrolling it to the top again and stay still.

Darius R.
  • 80
  • 7

1 Answers1

1

I've found the solution.

$(function() {
    $('a[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) {
            $('.content-container').animate({
                scrollTop: $('.content-container').scrollTop() + target.offset().top - 70
            }, 1000);
        return false;
        }
     }
   });
});  
Darius R.
  • 80
  • 7