Skip to content

Instantly share code, notes, and snippets.

@kimtrien
Created July 22, 2017 04:30
Show Gist options
  • Select an option

  • Save kimtrien/20b5eaabedb0f46ef6ce828185d8a473 to your computer and use it in GitHub Desktop.

Select an option

Save kimtrien/20b5eaabedb0f46ef6ce828185d8a473 to your computer and use it in GitHub Desktop.
Event scroll up and fixed header jquery
<header>
Header
</header>
<script>
$(document).ready(function () {
var lastScroll = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
// Make sure they scroll more than delta
if(Math.abs(lastScroll - st) <= 5)
return;
if (st < lastScroll) {
$("header").css("position", "fixed");
} else {
$("header").css("position", "absolute");
}
lastScroll = st;
});
});
</script>
<style>
header {
height: 100px;
border: 1px solid;
width: 100%;
margin: 0;
padding: 0;
background: white;
}
body {
height: 200vh;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment