Skip to content

Instantly share code, notes, and snippets.

Created September 19, 2013 00:33
Show Gist options
  • Save anonymous/6617699 to your computer and use it in GitHub Desktop.
Save anonymous/6617699 to your computer and use it in GitHub Desktop.
A Pen by Pavlo Mykhalov.
<div class="static">Static</div>
<div class="sticky">Sticky</div>
var sticky = document.querySelector('.sticky');
if (sticky.style.position !== 'sticky') {
var stickyTop = sticky.offsetTop;
document.addEventListener('scroll', function () {
window.scrollY >= stickyTop ?
sticky.classList.add('fixed') :
sticky.classList.remove('fixed');
});
}
.sticky {
position: sticky;
top: 0;
width: 100%;
padding: 1em 0;
background-color: #149bdf;
background-image: linear-gradient(-45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
background-size: 100px 100px;
color: white;
text-align: center;
}
.fixed {
position: fixed;
}
.static {
padding: 5em 0;
background: #1085bc;
color: white;
text-align: center;
}
body {
margin: 0;
}
:root {
height: 1000%;
font-family: sans-serif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment