In 2019 with CSS3 you can do this without Javascript at all. I frequently make sticky headers like this:
body {_x000D_
overflow-y: auto;_x000D_
margin: 0;_x000D_
}_x000D_
_x000D_
header {_x000D_
position: sticky; /* Allocates space for the element, but moves it with you when you scroll */_x000D_
top: 0; /* specifies the start position for the sticky behavior - 0 is pretty common */_x000D_
width: 100%;_x000D_
padding: 5px 0 5px 15px;_x000D_
color: white;_x000D_
background-color: #337AB7;_x000D_
margin: 0;_x000D_
}_x000D_
_x000D_
h1 {_x000D_
margin: 0;_x000D_
}_x000D_
_x000D_
div.big {_x000D_
width: 100%;_x000D_
min-height: 150vh;_x000D_
background-color: #1ABB9C;_x000D_
padding: 10px;_x000D_
}
_x000D_
<body>_x000D_
<header><h1>Testquest</h1></header>_x000D_
<div class="big">Just something big enough to scroll on</div>_x000D_
</body>
_x000D_