0

this script runs at the beginning of a website. If the user starts scrolling, some css animations start. After the classes are added (this all works so far), I want an auto-scroll to the next anchor.

The hasClass("makeVisible") == true part also works, but after scrolling to my anchor I can't scroll anymore. The page sticks.

Anybody an idea? Thanks in advance!

$(function() {
  $(window).scroll(function() {
    var scroll = $(window).scrollTop();
    if (scroll >= 100) {
      $('.navBg').addClass('activate');
      $('.logo h2').addClass('removeText');
      $('.logo').addClass('animate');
      $('.secondTitle').addClass('maker');
      setTimeout(function() {
        $('.animate').addClass('moveLeft');
      }, 500);
      setTimeout(function() {
        $('.maker').addClass('makeVisible');
      }, 700);

      if ($('.maker').hasClass("makeVisible") == true) {
        $('html, body').delay(300).animate({
          scrollTop: $('#secondPoint').offset().top
        }, 2000);

      } else {}
    } else {
      $('.animate').removeClass('moveLeft');
      $('.navBg').removeClass('activate');
      $('.logo h2').removeClass('removeText');
      $('.logo').removeClass('animate');
      $('.maker').removeClass('makeVisible');
      $('.secondTitle').removeClass('maker');
    }
  });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
cosmohorst
  • 63
  • 1
  • 7
  • I guess because once scrolled to `#secondPoint`, `scroll >= 100` is always `true`. Looks like a logic issue but hard to tell then what would be your expected behaviour – A. Wolff Nov 15 '17 at 10:03
  • The scroll event fires for almost every pixel the element is scrolled (depending on speed) so your logic is repeating far more than you are expecting. Your programatically scrolling the window will also fire the scroll event so you're getting stuck in a loop. You need a scroll end event such as https://stackoverflow.com/questions/3701311/event-when-user-stops-scrolling – Ian Lunn Nov 15 '17 at 10:07
  • Do you know another option how to autoscroll to an anchor after the script had added the classes? – cosmohorst Nov 15 '17 at 10:36

0 Answers0