Created
April 19, 2013 22:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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