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.
- //Scroll To Anchor Function
- $('a[href^="#"]').on('click', function(e) {
- var speed = 700;
- e.preventDefault();
-
- var target = this.hash;
- var $target = $(target);
- //On click hide (reverse) nav animation
- navAnimation.reverse();
- //Is location equal to the taget?
- if (location.hash != target) {
- //Scroll to and show hash
- setTimeout(function(){
- $('html, body').animate({
- 'scrollTop':$target.offset().top
- }, speed, 'swing', function() {
- window.location.hash = target;
- //When scroll has finished, reverse nav animation.
- navAnimation.play();
- });
- }, navLength);
- } else {
- console.log("im not animating");
- }
- });