title: How can I ignore scrolling when clicking an anchor link but listen to scrolling otherwise?
body: <p>Currently we have implemented a custom scrollspy because we couldn't get Bootstraps scrollspy to work properly.
  The scrollspy:</p>
  
  <pre><code>$(window).scroll(function () {
          var scroll = $(window).scrollTop(),
              /*offsets = hashes.forEach(function (hash) {
               return $(hash).offset().top;
               }),*/
              last;
          if (window.location.pathname === "/") {
              if (scroll &gt;= 20) {
                  $(".arrow").addClass("hidden");
              } else {
                  $(".arrow").removeClass("hidden");
              }
              hashes.forEach(function (hash) {
                  if (hash === "#contact") {
                      if ($(hash).offset().top - navheight - 10 &lt; scroll + 200) {
                          last = hash;
                      }
                  } else if ($(hash).offset().top - navheight - 10 &lt; scroll) {
                      last = hash;
                  }
              });
  
              hashes.forEach(function (hash) {
                  if (scroll &gt;= ($(".notfooter").outerHeight() - 60 - $("#contact").outerHeight() - ($("#partners").outerHeight() / 2))) {
                      $('li:has(a[href="#contact"])').addClass("testing");
                      $('li:has(a[href="#partners"])').removeClass("testing");
                  } else {
                      if (hash !== last) {
                          //console.log(hash + " not last hash");
                          $('li:has(a[href="' + hash + '"])').removeClass("testing");
                      } else if (hash === last) {
                          $('li:has(a[href="' + hash + '"])').addClass("testing");
                      }
                  }
              });
          }
      });
  </code></pre>
  
  <p>The hashes array is simply an array with all our anchor links in it.</p>
  
  <p>Then we have a class called <code>scrollto</code> to which we bind a click-event which then scrolls the window to the corresponding div.</p>
  
  <pre><code>$(".scrollto").on('click', function (event) {
          // Make sure this.hash has a value before overriding default behavior
          if (this.hash !== "") {
          // Prevent default anchor click behavior
          event.preventDefault();
  
          // Store hash
          var hash = this.hash;
  
          if (window.location.pathname !== "/") {
              $.get("/indexcontent", function (data) {
                      $("#bodycontent").html(data);
                      history.pushState(null, null, "/");
                      $('body').stop().animate({
                          scrollTop: $(hash).offset().top - navheight
                      }, 800, function () {
                          history.pushState(null, null, "");
                      });
                  }
              );
          } else {
              $('body').stop().animate({
                  scrollTop: $(hash).offset().top - navheight
              }, 800, function () {
                  history.pushState(null, null, "");
              });
          }
      }
  });
  </code></pre>
  
  <p>When a link is clicked it will first be marked as active (the class called <code>testing</code>) and we will start scrolling to that div. This is where the problem arises because as soon as we start scrolling the <code>$(window).scroll()</code> event triggers and will proceed to remove <code>testing</code> from the link we clicked and proceed to add and remove it from the links as we pass through them scrolling down the page.</p>
  
  <p>What I thought I could do was to unbind the scroll event at the start of the <code>scrollto</code> click event and then rebind it when the scrolling is done, but even if I did this in the callback of the <code>animate()</code> it would still say that it was done and bind it before the scrolling was actually done.</p>
  
  <p>So how should I think when trying to implement this? Is my reasoning about the bind/undbind trick in the right direction?</p>