Hoe OM - Versteek spyskaart op rol
Leer hoe om 'n navigasiekieslys te versteek wanneer jy afblaai met CSS en JavaScript.
Hoe om navigasiebalk te versteek wanneer u afrol
Stap 1) Voeg HTML by:
Skep 'n navigasiebalk:
Voorbeeld
<div id="navbar">
<a href="#home">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
</div>
Stap 2) Voeg CSS by:
Stileer die navigasiebalk:
Voorbeeld
#navbar {
background-color: #333; /* Black background
color */
position: fixed; /* Make it stick/fixed
*/
top: 0; /* Stay on top */
width: 100%; /* Full width */
transition: top 0.3s; /* Transition effect when sliding down (and up) */
}
/* Style the navbar links */
#navbar a {
float: left;
display: block;
color: white;
text-align: center;
padding: 15px;
text-decoration: none;
}
#navbar
a:hover {
background-color: #ddd;
color: black;
}
Stap 3) Voeg JavaScript by:
Voorbeeld
/* When the user scrolls down, hide the navbar. When the user scrolls up, show
the navbar */
var prevScrollpos = window.pageYOffset;
window.onscroll =
function() {
var currentScrollPos = window.pageYOffset;
if
(prevScrollpos > currentScrollPos) {
document.getElementById("navbar").style.top = "0";
} else {
document.getElementById("navbar").style.top = "-50px";
}
prevScrollpos = currentScrollPos;
}