Skip to content

Instantly share code, notes, and snippets.

@nvminhtu
Forked from mynameispj/props.txt
Created July 2, 2020 01:25
Show Gist options
  • Select an option

  • Save nvminhtu/7fff11c9a45811b557f252cb8e8b435c to your computer and use it in GitHub Desktop.

Select an option

Save nvminhtu/7fff11c9a45811b557f252cb8e8b435c to your computer and use it in GitHub Desktop.
Simple sticky elements with no jQuery plugin
http://daigo.org/2011/09/quick-and-dirty-sticky-elements-when-scrolling-using-jquery/
$(window).scroll(function(){
var offset = 0;
var sticky = false;
var top = $(window).scrollTop();
if ($("#wrapper").offset().top < top) {
$("#must-see").addClass("sticky");
sticky = true;
} else {
$("#must-see").removeClass("sticky");
}
});
.sticky {
position:fixed; top:0;
}
<div id="wrapper">
<div id="must-see">
This will stick when you scroll!
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment