Jump to content

Javascript for Header help

Recommended Posts

site: https://grouper-azalea-byr2.squarespace.com/

password: lara

 

Hi! 

I am using a javascript code in my footer code injection area that was given to me by a member here on the forum. However, I am having a tiny issue with it but haven't heard back from the OG member, so starting a new thread in hopes of solving this for my client. My request was to: hide the site title of the first page only, first section only (site title appearing after scrolling past the first section). The code that was given to me works GREAT. However, the code does this for every page of the site, when I want it active only on the homepage. Can someone help me make this happen? Code down below: 

 

<script>
const logo = document.getElementById('site-title'); // get reference to sitelogo element
const landingSection = document.querySelector('#page #sections section:first-of-type'); 

const observer = new IntersectionObserver(entries => {
  entries.forEach(entry => {
    if (entry.target === landingSection) {
      if (!entry.isIntersecting && entry.intersectionRatio === 0) {
        logo.classList.remove('hidden'); 
      } else {
        logo.classList.add('hidden'); 
      }
    }
  });
});

observer.observe(firstSection); 
</script>

<style>
  .hidden {
    visibility: hidden;
  }
</style>


 

Link to comment
  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

A few days ago there was a similar question, but for logo. I have posted the code there, you can try searching on the Forum. If you still can't find it, let me know, I can give new code

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

How to: Setup Password & Share url Insert Custom CSS - Page Header - Upload Custom Font - Upload File - Find Block ID - Contact Customer Care

Link to comment
  • 2 weeks later...
Posted (edited)
On 5/3/2023 at 6:49 PM, kaydotjpg said:

The code that was given to me works GREAT. However, the code does this for every page of the site, when I want it active only on the homepage.

The code is missing a section that checks if the page has loaded, which is why it didn’t work when you tried it in the per-page header injection. It will load too early. You could move it to a Code Block instead, placed at the very bottom of the homepage, to give the content time to load.

Alternatively, you could prefix the selectors in the querySelector method to include the .homepage class. This would ensure it only works when the class is included. 

Edited by paul2009

About: SQSP User for 17 yrs. Circle Leader since 2017. I value honesty, transparency, diversity and good design ♥.
Work: Founder of SF Digital, building Squarespace Extensions to supercharge your commerce website. 
Content: Links in my posts may refer to SF Digital products or may be affiliate links.
If my advice helped, you can thank me by clicking one of the feedback emojis below. I love coffee too.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.