I have a page configured with a link to the top link (in the entire panel), slides down at the top of the page, only when scrolling up.
I start with Javascript, so I put something together, it really works (it shows when scrolling up, if we are not 500px from the top, it doesnβt hide scrolling down), and it uses some code that I got from here to not check the scrolling each pixel.
What I want to add is that even if you are still scrolling, as soon as you get into the submenu, then toplink should scroll the page again - I do not need to return to the top when you are at the top,
I got this by adding a SECOND Javascript script, but I know there must be a much better way to get something like this to work on the first call. Also this second call uses the window.scroll function, which I'm sure is the wrong way to do this.
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>
HTML and CSS script: https://jsfiddle.net/Lu0jz3nc/11/
source share