We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

How can I create a transparent homepage header that turns white when scrolling?

Solved

How can I create a transparent homepage header that turns white when scrolling?

Marco211
Explorer
95 0 21

Hi all, do you know how to make the header of my website only in homepage transparent and then when it goes down white? Thanks a lot to everyone

 

https://refade-8966.myshopify.com/

password: refade

Accepted Solution (1)

webwondersco
Shopify Partner
1204 172 173

This is an accepted solution.

@Marco211 , Please add the below line CSS and Js at theme.liquid file. just before the ending of </body> tag.

 

{% if template == 'index' %}
<style>
.header {
  position: fixed;
  top: 40px;
  left: 0;
  width: 100%;
  z-index: 999;
  transition: background-color 0.3s ease;
}

.header.transparent {
  background-color: transparent;
}

.header.white {
  background-color: #ffffff;
top:0;
}

</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
  var header = document.querySelector('.header');
  header.classList.add('transparent');
});

// Change header background on scroll
window.addEventListener('scroll', function() {
  var header = document.querySelector('.header');
  var scrollTop = window.pageYOffset || document.documentElement.scrollTop;

  if (scrollTop > 0) {
    header.classList.remove('transparent');
    header.classList.add('white');
  } else {
    header.classList.remove('white');
    header.classList.add('transparent');
  }
});
</script>
{% endif %}

 

Web Wonders | Shopify Partner | Connect here!
- Question answered? Mark as Accepted Solution, reply helpful? Click Like
- Hire me, if you want to design, re-design, develop a store, or make changes to the pre-built store.
- a small Coffee Tip would be greatly appreciated. :)..! Instagram

View solution in original post

Replies 2 (2)

webwondersco
Shopify Partner
1204 172 173

This is an accepted solution.

@Marco211 , Please add the below line CSS and Js at theme.liquid file. just before the ending of </body> tag.

 

{% if template == 'index' %}
<style>
.header {
  position: fixed;
  top: 40px;
  left: 0;
  width: 100%;
  z-index: 999;
  transition: background-color 0.3s ease;
}

.header.transparent {
  background-color: transparent;
}

.header.white {
  background-color: #ffffff;
top:0;
}

</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
  var header = document.querySelector('.header');
  header.classList.add('transparent');
});

// Change header background on scroll
window.addEventListener('scroll', function() {
  var header = document.querySelector('.header');
  var scrollTop = window.pageYOffset || document.documentElement.scrollTop;

  if (scrollTop > 0) {
    header.classList.remove('transparent');
    header.classList.add('white');
  } else {
    header.classList.remove('white');
    header.classList.add('transparent');
  }
});
</script>
{% endif %}

 

Web Wonders | Shopify Partner | Connect here!
- Question answered? Mark as Accepted Solution, reply helpful? Click Like
- Hire me, if you want to design, re-design, develop a store, or make changes to the pre-built store.
- a small Coffee Tip would be greatly appreciated. :)..! Instagram
ThompsonLane
Tourist
10 0 4

Hello!

 

I am hoping to achieve this same effect on my site but not having any luck with the above code. I'd love some help if possible? 🙂

 

URL: www.thompsonlane.com.au

Template: Split

PW: siteloading