Skip to content

Instantly share code, notes, and snippets.

@alfahami
Created March 11, 2020 17:58
Show Gist options
  • Save alfahami/87b383e40bbd1eab69d2d589a4721679 to your computer and use it in GitHub Desktop.
Save alfahami/87b383e40bbd1eab69d2d589a4721679 to your computer and use it in GitHub Desktop.
Keep the background color of the navbar after reload without using cookies
window.addEventListener("scroll", function() {
if (window.scrollY > 150) {
document.querySelector("#jsc-nav").style.background = "rgba(0,0,0,0.9)";
} else {
document.querySelector("#jsc-nav").style.background = "transparent";
}
});
// Scroll and keep the background navbar color after reload
$(document).ready(function() {
checkHeaderStatus();
$(window).scroll(function() {
checkHeaderStatus();
});
});
function checkHeaderStatus() {
var navbar = $("#jsc-nav");
var scrollPosition = $(window).scrollTop();
if (scrollPosition < 150) {
navbar.css("background-color", "transparent");
} else {
navbar.css("background-color", "rgba(0,0,0,0.9)");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment