I am creating a visible and hidden effect of a div fixed
by doing scroll
. In conjunction with the header
and another div sticky
.
Fixed div is hidden when scrolling but when finished displaying the header: using outerHeight
.
What I can't do is change the background color of the fixed div while it's visible ( top: 0
) but after passing the header. Being on the header, it must have a transparent background (or be able to add other styles).
My problem is not to apply the style with classList.add
or style.background = ""
but, how to capture the moment when it is visible but outside the header.
For example I want this: if the scroll is greater than the header, then apply background color to the fixed divo (which is visible)
if ( scroll > header ) headland.style.background = "red"
UPDATED
Apart from changing the background color (which I can achieve), I need it to headland
be visible along with the navigation ( sticky
) when scrolling top
(up), but not while it header
is visible
document.addEventListener("DOMContentLoaded", () => {
let scroll = window.pageYOffset,
headland = document.querySelector(".headland"),
navegacion = document.querySelector(".navegacion");
window.addEventListener("scroll", () => {
let s = window.pageYOffset,
w = window.outerHeight - 70;
if(scroll < s && s > w) {
headland.classList.add("top");
//headland.classList.add("fondo");
navegacion.classList.remove("top");
}
/* else if(scroll > w) {
headland.classList.add("fondo");
} */
else {
headland.classList.remove("top");
headland.classList.remove("fondo");
navegacion.classList.add("top");
}
scroll = s;
});
});
* {
box-sizing: border-box
}
:root {
--height: 50px;
--transicion: all 0.3s ease-in-out;
--color: lightblue;
--fondoVerde: rgb(47, 79, 79);
--fondoNegro: rgb(0, 0, 0);
--fondoVerdeTransparente: rgba(47, 79, 79, 0.5);
}
html {
scroll-behavior: smooth
}
body {
margin: 0;
padding: 0;
font-size: 16px;
background-color: whitesmoke
}
.headland, header, .navegacion {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
margin: 0;
padding: 1rem 1rem;
}
.headland {
position: fixed;
z-index: 1000;
top: 0;
left: 0;
height: var(--height);
transition: var(--transicion);
color: var(--color);
box-shadow: 0 0 7px #ccc;
background-color: transparent;
backdrop-filter: blur(3px)
} .headland.top {
top: -50px;
} .headland.fondo {
background-color: var(--fondoVerde)
}
header {
position: relative;
height: 100vh;
color: var(--color);
background-color: var(--fondoNegro)
}
.navegacion {
position: sticky;
top: 0;
z-index: 1001;
transition: var(--transicion);
background-color: var(--fondoVerdeTransparente);
backdrop-filter: blur(3px)
} .navegacion.top {
top: var(--height)
}
.navegacion button {
margin: auto
}
main {
min-height: 1000px
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=UTF-8">
<title>Scroll</title>
</head>
<body>
<div class="headland" id="headland">
<h1>Página de prueba</h1>
</div>
<header>
<h1>Header</h1>
</header>
<div class="navegacion" id="navegacion">
<button>Botón</button>
<button>Botón</button>
<button>Botón</button>
</div>
<main></main>
</body>
</html>