Execute/Stop Script
-
I would like to use this first script:
var reachedFromTop = false; var reachedFromBottom = false; $(window).scroll(function() { //After scrolling 100px from the top... if ($(window).scrollTop() > 100 ) { if (!reachedFromTop) something(); reachedFromTop = true; } else if (reachedFromTop && otherCondition) { if (!reachedFromBottom) somethingElse(); reachedFromBottom = true; } });
to execute the script bellow once the user has scrolled down to my nav bar and stop the script once the user has scrolled back up to my nav bar.
var didScroll; var lastScrollTop = 0; var delta = 5; var navbarHeight = $('.main-navigation').outerHeight(); $(window).scroll(function (event) { didScroll = true; }); setInterval(function () { if (didScroll) { hasScrolled(); didScroll = false; } }, 250); function hasScrolled() { var st = $(this).scrollTop(); // Make sure they scroll more than delta if (Math.abs(lastScrollTop - st) <= delta) return; // If they scrolled down and are past the navbar, add class .nav-up. if (st > lastScrollTop && st > navbarHeight) { // Scroll Down $('.main-navigation').removeClass('nav-down').addClass('nav-up'); } else { // Scroll Up if (st + $(window).height() < $(document).height()) { $('.main-navigation').removeClass('nav-up').addClass('nav-down'); } } lastScrollTop = st; }
I have tried different set up, however, I can’t get it work. The error that is returned is simple, nothing happen. I don’t have any specific message showing up in firebug.
What I have tried so far to do is to replace the something/somethingElse in the first script by setInterval. Also, in my JavaScript file I place the first script at the beginning of my file.
- The topic ‘Execute/Stop Script’ is closed to new replies.