vincentlefre Posted August 24, 2022 Share Posted August 24, 2022 Hi, I currently have a header that disappears on scroll down and reappears on scroll up. It's working really well using the code below. However I would also like the header to reappear when the user moves the cursor near to the top of the viewport. Can this be done with javascript? site: https://adrisenergy.squarespace.com/ password: 1234 Any help greatly appreciated // Hide Header on on scroll down var didScroll; var lastScrollTop = 0; var delta = 5; var navbarHeight = $('header').outerHeight(); $(window).scroll(function(event){ didScroll = true; }); setInterval(function() { if (didScroll) { hasScrolled(); didScroll = false; } }, 50); 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. // This is necessary so you never see what is "behind" the navbar. if (st > lastScrollTop && st > navbarHeight){ // Scroll Down $('header').removeClass('nav-down').addClass('nav-up'); } else { // Scroll Up if(st + $(window).height() < $(document).height()) { $('header').removeClass('nav-up').addClass('nav-down'); } } lastScrollTop = st; } Link to comment
Beyondspace Posted August 30, 2022 Share Posted August 30, 2022 On 8/24/2022 at 10:48 PM, vincentlefre said: Hi, I currently have a header that disappears on scroll down and reappears on scroll up. It's working really well using the code below. However I would also like the header to reappear when the user moves the cursor near to the top of the viewport. Can this be done with javascript? site: https://adrisenergy.squarespace.com/ password: 1234 Any help greatly appreciated // Hide Header on on scroll down var didScroll; var lastScrollTop = 0; var delta = 5; var navbarHeight = $('header').outerHeight(); $(window).scroll(function(event){ didScroll = true; }); setInterval(function() { if (didScroll) { hasScrolled(); didScroll = false; } }, 50); 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. // This is necessary so you never see what is "behind" the navbar. if (st > lastScrollTop && st > navbarHeight){ // Scroll Down $('header').removeClass('nav-down').addClass('nav-up'); } else { // Scroll Up if(st + $(window).height() < $(document).height()) { $('header').removeClass('nav-up').addClass('nav-down'); } } lastScrollTop = st; } You can refer the following javascript event: https://developer.mozilla.org/en-US/docs/Web/API/Element/mousemove_event vincentlefre 1 Greeting, it's BeyondSpace, I am Squarespace dev focus on provide solutions to enhance feature that squarespace.com can't provide.Feel free to check my current Squarespace Plugins Developement: Enable Pinch/Zoom on lightbox, Delivery Date Picker, Lightbox Studio pluginIf you find my answer fit your need, let's leave a like or upvote so others with the same issue can find their solution. Thank you Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment