Created
February 14, 2012 13:07
-
-
Save mbjordan/1826661 to your computer and use it in GitHub Desktop.
Simple jQuery Sticky-on-Scroll Navigation
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
#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; | |
} |
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
<div id="wrapper"> | |
<aside> | |
<p>Content to keep on the side and at the top when scrolled.</p> | |
</aside> | |
</div> |
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
/** | |
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