scroll to Anchor - prevent animation on section

scroll to Anchor - prevent animation on section

Hi all,

Im new to the forum (and somewhat of a beginner to jQuery), and have stumbled onto a problem with a scroll to anchor function.

At the moment, when the user clicks an element on the nav bar, the nav bar hides via a GSAP timeline, it scrolls down to the section, then the GSAP timeline reverses the nav animation so it re-appears.

What I'm stuck on - is that I cant seem to prevent the navigation animation happening when the user is already at that particular part of the page.
For example, if you are on home, and click home, the nav animation shouldn't play, or at least do something else.

What I have tried:
  • Trying to see if the location.hash equals the target (no luck).
  • Clearing location.hash
Many thanks in advance.
  1.     //Scroll To Anchor Function
  2.     $('a[href^="#"]').on('click', function(e) {
  3.         var speed = 700;
  4.         e.preventDefault();
  5.     
  6.         var target = this.hash;
  7.         var $target = $(target);

  8.         //On click hide (reverse) nav animation
  9.         navAnimation.reverse();

  10.         //Is location equal to the taget?
  11.         if (location.hash != target) {
  12.             //Scroll to and show hash
  13.             setTimeout(function(){ 
  14.                 $('html, body').animate({
  15.                     'scrollTop':$target.offset().top
  16.                 }, speed, 'swing', function() {
  17.                     window.location.hash = target;
  18.                     //When scroll has finished, reverse nav animation.
  19.                     navAnimation.play();
  20.                 });
  21.             }, navLength);
  22.         } else {
  23.             console.log("im not animating");
  24.         }
  25.     });