Skip to content

Instantly share code, notes, and snippets.

@cherihung
Created March 18, 2013 20:43
Show Gist options
  • Save cherihung/5190636 to your computer and use it in GitHub Desktop.
Save cherihung/5190636 to your computer and use it in GitHub Desktop.
make an element on page "stick" while rest of page scrolls
$(document).ready(function () {
$(window).scroll(function(e){
$element = $('#element-id');
var init_height = 250;
if ($(this).scrollTop() > init_height && $element.css('position') != 'fixed'){
$('#element-id').css({'position': 'fixed', 'top': '0px'});
} else if ($(this).scrollTop() < init_height && $element.css('position') == 'fixed'){
$('#element-id').css({'position': 'relative'});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment