Skip to content

Instantly share code, notes, and snippets.

@verteb
Created April 11, 2013 13:53
Show Gist options
  • Select an option

  • Save verteb/5363561 to your computer and use it in GitHub Desktop.

Select an option

Save verteb/5363561 to your computer and use it in GitHub Desktop.
jQuery sticky element
<style>
#sticky.stick {
position: fixed;
top: 0;
z-index: 99999999999;
}
</style>
<script>
function sticky_relocate() {
var window_top = $(window).scrollTop();
var div_top = $('#sticky-anchor').offset().top;
if (window_top > div_top) {
$('#sticky').addClass('stick');
} else {
$('#sticky').removeClass('stick');
}
}
$(function() {
$(window).scroll(sticky_relocate);
sticky_relocate();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment