- This topic has 18 replies, 3 voices, and was last updated 4 years, 11 months ago by Leo.
-
AuthorPosts
-
April 25, 2019 at 2:12 pm #879671AJ
Hi, I was hoping to get some help on this buildout.
I’d like the front page header, site logo + nav to be hidden until the user scrolls past the background image in the first section.
Very similar to what this user wanted to do. However, it’s a bit outdated and I’m not sure where to begin.
https://generatepress.com/forums/topic/hide-nav-top-menu-logo-until-scroll/Have GP Premium + Elementor Pro.
Super noob when it comes to any custom CSS or JS.
Thanks!
April 25, 2019 at 4:11 pm #879753TomLead DeveloperLead DeveloperIt looks like you’re using Elementor to build your header, so we can’t use anything built into GP to do this.
I think you would have to use custom javascript to detect when the first section has been scrolled passed.
Custom javascript is hard, and a not really within the scope of forum support unfortunately.
After a quick search, you could maybe try this CSS:
.elementor-location-header { display: none; } .elementor-location-header.show-me { display: none; }
Then this javascript:
window.addEventListener("scroll", function() { var elementTarget = document.querySelector( '.elementor-location-header' ); if ( window.scrollY > ( elementTarget.offsetTop + elementTarget.offsetHeight ) ) { elementTarget.classList.add( 'show-me' ); } else { elementTarget.classList.remove( 'show-me' ); } } );
Not sure if it will work, but it’s worth a shot.
April 26, 2019 at 7:49 am #880542AJWhere/which file would you add the JavaScript to?
April 26, 2019 at 9:30 am #880655TomLead DeveloperLead DeveloperYou could:
1. Create a Hook Element: https://docs.generatepress.com/article/hooks-element-overview/
2. Set the hook aswp_footer
3. Add this as the hook content:<script> window.addEventListener( 'scroll', function() { var elementTarget = document.querySelector( '.elementor-location-header' ); if ( window.scrollY > ( elementTarget.offsetTop + elementTarget.offsetHeight ) ) { console.log('show'); elementTarget.classList.add( 'show-me' ); } else { console.log('hide'); elementTarget.classList.remove( 'show-me' ); } } ); </script>
April 26, 2019 at 11:41 am #880779AJHrmmm. Implemented the CSS as well as the JavaScript using the hook element.
Didn’t work.
It did hide the header, though. It just won’t appear during scroll.
I know you said that custom javascript is hard, and a not really within the scope of forum support, but any additional help would be greatly appreciated. If not, I understand.
Thanks again, you rock either way!
April 26, 2019 at 6:53 pm #881043TomLead DeveloperLead DeveloperI just added some debugging stuff to the script above: https://generatepress.com/forums/topic/hide-header-site-logo-nav-menu-until-scroll-on-front-page/#post-880655
Can you re-add it and let me know so I can take a look?
Thanks!
April 27, 2019 at 6:21 am #881343AJadded
April 27, 2019 at 7:43 am #881519TomLead DeveloperLead DeveloperIs it possible to give your Elementor header an ID?
April 27, 2019 at 9:33 am #881586AJAdded elementor-location-header as the ID.
However, would this be infinitely easier and more productive if we used the GP header?
The header layout I’m wanting is pretty simple.
Logo on the left. full-width nav.
Haven’t decided what I want it to look like on mobile, yet.
April 27, 2019 at 9:56 am #881597April 27, 2019 at 3:29 pm #881763TomLead DeveloperLead DeveloperI’m not seeing an ID on the containing element. If you used the GP header we could try using the ID that comes with it.
April 27, 2019 at 4:39 pm #881803AJAlright. I’ve disabled the Elementor header and I am using the GP header.
I currently have this JavaScript at Hook Element set as wp_footer
<script> window.addEventListener("scroll", function() { var elementTarget = document.querySelector( '.elementor-location-header' ); if ( window.scrollY > ( elementTarget.offsetTop + elementTarget.offsetHeight ) ) { elementTarget.classList.add( 'show-me' ); } else { elementTarget.classList.remove( 'show-me' ); } } ); </script>
I then have the following CSS
.elementor-location-header { display: none; } .elementor-location-header.show-me { display: none; }
I’m sure we’re going to have to tweak this a bit now.
April 28, 2019 at 8:13 am #882365TomLead DeveloperLead DeveloperLet’s try this instead:
#site-navigation, #sticky-navigation { opacity: 0; visibility: hidden; height: 0; overflow: hidden; } #sticky-navigation.show-me { opacity: 1; visibility: visible; height: auto; overflow: visible; }
Then add this as your javascript:
<script> var topofDiv = jQuery( '.elementor-element-c023fe1' ).offset().top; var height = jQuery( '.elementor-element-c023fe1' ).outerHeight(); jQuery( window ).scroll( function() { if ( jQuery( window ).scrollTop() > ( topofDiv + height ) ) { jQuery( '#sticky-navigation' ).addClass( 'show-me' ); } else { jQuery( '#sticky-navigation' ).removeClass( 'show-me' ); } } ); </script>
April 28, 2019 at 9:37 am #882434AJUpdated the code.
Header/Nav is now hidden but does not reappear when scrolling past the first section.
April 28, 2019 at 2:58 pm #882572TomLead DeveloperLead DeveloperGetting closer I think! Can you try the updated code?
-
AuthorPosts
- You must be logged in to reply to this topic.