Skip to content

Instantly share code, notes, and snippets.

@snadrus
Created June 12, 2015 04:40
Show Gist options
  • Save snadrus/21a1c274fff8b3f7f834 to your computer and use it in GitHub Desktop.
Save snadrus/21a1c274fff8b3f7f834 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload=function() {
var header = document.querySelector('.header');
isScrolled = false;
window.addEventListener("scroll", function(ev) {
if (!document.body.scrollTop) {
if (isScrolled) {
header.classList.remove('scrolled');
isScrolled = false;
}
}else if (!isScrolled) {
header.classList.add('scrolled');
isScrolled = true;
}
});
}
</script>
<style>
html, body { margin: 0;}
.header {
font-size: 24px;
height: 80px;
background-color: #fca;
transition: all 5000ms ease-in-out;
}
.header.scrolled {
background-color: rgba(0,0,200,0.5);
position: fixed;
top: 0;
width: 100%;
height: 40px;
font-size: 18px;
color: green;
}
</style>
</head>
<body>
<div class=header>
This is a static header at the top of a page that becomes something else when scrolled.
</div>
<br><br>lj;<br><br><br><br>lkjlkj<br><br>lkjkl<br><br><br>lkjlkj<br><br><br><br>jklj<br><br><br><br><br>lkj<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>lkjl<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment