Skip to content

Instantly share code, notes, and snippets.

@mbjordan
Created February 14, 2012 13:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbjordan/1826661 to your computer and use it in GitHub Desktop.
Save mbjordan/1826661 to your computer and use it in GitHub Desktop.
Simple jQuery Sticky-on-Scroll Navigation
#wrapper { position: relative; }
/* Aside in non-scroll mode */
aside {
float: left; /* Keeps the aside to the left and on top */
margin-top: 5px;
padding: 15px 0 15px 8px;
width: 267px;
}
<div id="wrapper">
<aside>
<p>Content to keep on the side and at the top when scrolled.</p>
</aside>
</div>
/**
Set the scroll events for the HTML5 Scroll Load plugin and keeping the aside always in view.
*/
var asideTop = $('aside').offset().top - parseFloat($('aside').css('marginTop').replace(/auto/, 0));
$(window).scroll(function () {
var asideY = $(this).scrollTop();
if (asideY >= asideTop) {
$('aside').css({
position: 'fixed',
top: '0px'
})
} else {
$('aside').removeAttr('style')
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment