Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Code Snippet Position-sticky - ZhuFeng</title>
</head>
<body>
<p style="margin-bottom:100px;">Scroll this page.</p>
<div class="sticky"><h3>Super amazing header</h3></div>
<p style="margin-top:500px;">Still there?</p>
<p style="margin-top:500px;">Yep!</p>
<p style="margin-top:500px;">Scroll so hard!</p>
</body>
</html>
 
body {
  margin: 0;
  text-align: center;
  font-family: sans-serif;
}
.fixed {
  position: fixed;
  top: 0;
}
.sticky {
  width: 100%;
  background: #F6D565;
  padding: 25px 0;
  text-transform: uppercase;
}
 
var header = document.querySelector('.header'); 
var origOffsetY = header.offsetTop; 
function onScroll(e) { 
    window.scrollY >= origOffsetY ? header.classList.add('sticky') : header.classList.remove('sticky'); 
} 
document.addEventListener('scroll', onScroll);
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
taoqianbaopro
0viewers