Is there a way to dynamically change the color of my header based on the background color as I scroll through different sections of my webpage? I have a fixed header and multiple sections with varying background colors. I want the header's text color to change for better readability when it overlaps with a section of a different background color. However, I'm unsure of how to achieve this effect. I've looked online for solutions but haven't found anything that addresses this specific issue.
Here's what I have tried so far: (check out this JSFIDDLE)
class Div extends React.Component{
constructor() {
super()
this.state = {
headerClass: 'white'
}
}
changeColor() {
// something like
this.setState({ headerClass: 'black'})
}
render(){
return(
<div>
<div id="header">
<h1 className={`${this.state.headerClass}`}>
This is the header
</h1>
</div>
<div id="section_1" className="section">
This is section 1
</div>
<div id="section_2" className="section">
This is section 2
</div>
<div id="section_3" className="section">
This is section 3
</div>
<div id="section_4" className="section">
This is section 4
</div>
<div id="section_5" className="section">
This is section 5
</div>
</div>
)
}
}
Here's the CSS:
#main {
height: 2000px;
position: relative;
}
#section_1 {
background: grey;
}
.section {
height: 400px;
background: white;
padding: 30px 0;
}
#header {
height: 50px;
background: transparent;
position: fixed;
width: 100%;
left: 0;
top: 0;
right: 0;
z-index: 1
}
h1 {
color: white;
}
Any suggestions or hints on how to achieve this effect?