Skip to content

Instantly share code, notes, and snippets.

@drainpip
Created April 19, 2013 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drainpip/5423761 to your computer and use it in GitHub Desktop.
Save drainpip/5423761 to your computer and use it in GitHub Desktop.
Assumes jQuery. This will detect the position of your ID, check if it's got a margin on the top and remove it. If the ID reaches the top of the screen, then it will add a class fixed. Obviously you would need to make position:fixed; part of the fixed class, plus add in the actual fixed position.
$(function () {
var top = $('#ID').offset().top - parseFloat($('#ID').css('margin-top').replace(/auto/, 0)); // math is there in case the element has margin-top and removes it
$(window).scroll(function (event) {
// what the y position of the scroll is
var y = $(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
$('#menu').addClass('fixed');
} else {
// otherwise remove it
$('#menu').removeClass('fixed');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment