Skip to content

Instantly share code, notes, and snippets.

@mynameispj
Created February 8, 2013 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mynameispj/4742342 to your computer and use it in GitHub Desktop.
Save mynameispj/4742342 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