To achieve the desired effect, you need to follow these steps.
Start by setting the position of your header in the CSS to fixed
.
Next, adjust the transparency settings by adding something like opacity: 0.5
. For more control over both color and transparency, consider using RGBA values like
background-color: rgba(255, 0, 0, 0.3)
Finally, attach a scroll event handler using JavaScript. If jQuery is available, a simple implementation can be:
$("#header").scroll(myScrollHandler);
function myScrollHandler () {
if (window.pageYOffset > 50)
{
$("#header").fadeTo(200, 0.9);
}
else
{
$("#header").fadeTo(200, 0.3);
}
}
In this example, sample values are used for duration and opacity levels. The comparison checks the page offset against the number of pixels before the header becomes visible.
Summarizing, ensure your CSS includes:
#header {
position: fixed;
top: 0;
width: 100%;
background-color: rgba(255, 0, 0, 0.3);
}
In your HTML, include:
<script>
$("#header").scroll(myScrollHandler);
function myScrollHandler () {
if (window.pageYOffset > 50)
{
$("#header").fadeTo(200, 0.9);
}
else
{
$("#header").fadeTo(200, 0.5);
}
}
</script>
<div id="header">
Your header content
</div>