Skip to content

Instantly share code, notes, and snippets.

@takien
Created December 15, 2012 03:31
Show Gist options
  • Save takien/4291131 to your computer and use it in GitHub Desktop.
Save takien/4291131 to your computer and use it in GitHub Desktop.
/*static.js
* @author: takien
* @link: http://takien.com
* static element uses jQuery, no CSS required.
* Usage: add class 'element-static' to your element to make it static
*/
jQuery(document).ready(function($){
function element_static(scroll){
$('.element-static').each(function() {
var left = $(this).offset().left;
var offsettop = $(this).offset().top;
if(scroll != false){
offsettop = $(this).parent('.element-static-wrap').offset().top;
}
if($(window).scrollTop() >= offsettop){
$(this).css({
'position' :'fixed',
'top' : 0
});
var stop = $(this).data().stop;
if(stop != undefined) {
var bottom = $(this).height();
var stopoffset = $(stop).position().top-$(window).scrollTop()-bottom;
if(stopoffset < bottom){
$(this).css({
'position' :'fixed',
'top' : stopoffset
});
}
}
}
else{
$(this).css({
'position' :'inherit',
});
}
});
}
$('.element-static').wrap('<div class="element-static-wrap" />');
$(window).scroll(function() {
element_static(true);
});
$(window).resize(function() {
element_static(false);
});
element_static(false);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment