I have a page set up with a back to top link (in an entire bar) slides down at the top of the page, only on scroll up.
I'm a beginner with Javascript, so I have cobbled something together, that does work (it shows on scroll up, unless we're 500px from the top, hides on scroll down), and it uses some code I got from here to not check every pixel scrolled.
What I want to add is that even if you are still scrolling up once you get to the submenu, then the toplink should scroll off the page again - I don't need a back to the top once you are at the top.
I've gotten this to work by adding a SECOND Javascript script, but I know that there must be a much better way to get something like this to work in the first call. Also this second call uses the window.scroll function which I'm pretty sure is the wrong way to do it.
FIRST SCRIPT
// Hide Header on on scroll down var didScroll; var lastScrollTop = 0; var delta = 5; var subMenu = $('#subMenu').offset().top; $(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 scroll down, add class .nav-up. if (st > lastScrollTop && st ){ // Scroll Down $('#backtotop').removeClass('nav-down').addClass('nav-up'); $(".toplink").fadeOut("fast"); } else { // Scroll Up if(st + $(window).height() < $(document).height() && st > 500) { $('#backtotop').removeClass('nav-up').addClass('nav-down'); $(".toplink").fadeIn("slow"); } } lastScrollTop = st; } SECOND SCRIPT
<script> $(window).scroll (function () {var st = $(this).scrollTop(); var subMenu = $('#subMenu').offset().top; if (st < subMenu) { $('#backtotop').removeClass('nav-down').addClass('nav-up'); $(".toplink").fadeOut("fast"); } }); </script> Fiddle with the HTML and CSS: https://jsfiddle.net/Lu0jz3nc/11/
hasScrolledwhen you click theto topbutton